简体   繁体   中英

print javascript variable as an attribute in html tag

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?

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>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM