1

I have the following code;

var isEditable = !self.model.hasPrivilege("UPDATE_INFO") || !self.editInfo;

return "<input type='checkbox' name='flag' class='flag' />";

Now my question is while doing the return for the input, can I add condition for isEditable as part of the same return "<input type='checkbox'

Basically I want to return disabled checkbox if isEditable is false and vice-versa ?

2 Answers 2

2
return '<input type="checkbox" ' + (isEditable ? 'disabled="disabled"' : '') + ' .../>';
Sign up to request clarification or add additional context in comments.

1 Comment

a small correction :) return '<input type="checkbox" ' + (!isEditable ? 'disabled="disabled"' : '') + ' .../>'; He wants to disable if isEditable is false
0

I think if this is jQuery, you can make it more clear like this:

var isEditable = !self.model.hasPrivilege("UPDATE_INFO") || !self.editInfo, 
    $checkBox = $("<input />", {
        type : "checkbox",
        name : "flag",
        class : "flag",
        disabled : !isEditable // condition to make the returned checkbox disabled
    });

return $checkBox;

this way, even if want to bind some events to the returned checkbox element, you can do that via adding another key to the second parameter object like:

$checkBox = $("<input />", {
    type : "checkbox",
    name : "flag",
    class : "flag",
    disabled : !isEditable, // condition to make the returned checkbox disabled,
    click : doSomething // attached some event
});

Comments

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.