0
var disabled = '';
if(value.action.length === 0)
{
  disabled = 'disabled="disabled"';
}
row += '<td><input type="checkbox" name="create" value="1" class="checkbox" data-action="'+ value.action + '" data-controller = "'+ value.controller +'" data-role="'+ role +'" document.write(disabled)></td>';

I want to print disabled="disabled" based on the truthfulness of my condition. In PHP this can be easily done by the method I used above but I failed to to it in JavaScript. The document.write() is getting printed as it is.

How can I do this in JavaScript?

1
  • You don't need document.write here, simply concat the strings as you did before: … + '" ' + disabled + '></td>'; or even with less code: … + '"' + ( value.action.length === 0 ? ' disabled' : '' ) + '></td>'; Commented Mar 13, 2015 at 10:06

1 Answer 1

1

You don't need document.write. Just like this:

if(value.action.length === 0)
{
    disabled = 'disabled="disabled"';
}else{
    disable = '';
}
row += '<td><input type="checkbox" name="create" value="1" class="checkbox" data-action="'+ value.action + '" data-controller = "'+ value.controller +'" data-role="'+ role +'" '+ disabled +'></td>';
Sign up to request clarification or add additional context in comments.

4 Comments

Yep, and disabled="disabled" is unnecessary. just "disabled" would suffice, because it's a HTML bool. disabled="false" would also disable it.
so: var disabled = (value.action.length ===0) ? "disabled" : "";
thanks for pointing out, I focused too much on this contatenation. Already edited.
yes i done that. That was a silly thing i was doing :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.