I have this code:
<input type='checkbox'>
I want to make conditional using jQuery, for example, if data[1].condition is true, then set it checked, if not then not.
May be something like:
<input type='checkbox' + if data[1].condition is false, unchecked + "'>
<input type='checkbox' + if data[1].condition is true, checked + "'>
Is there a way to set it inline? Something like:
<input type='checkbox' + data[1].condition | checked + "'>
Simple answer: No!
You cannot do this using HTML and JS the way you are using it. However, if you use a templating library like EJS then it can be done inline as you want to.
<input type="checkbox" <%= data[1].condition ? 'checked' : '' %> />
The other option is to use JS/jQuery to set the property using a script as done by Brad above.
There's not really a way to do this inline.
Using jQuery...
if(data[1].condition === false){
$('input').removeAttr('checked');
} else {
$('input').prop('checked', true);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With