Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

True-False condition inline on HTML element (checkbox)

Tags:

html

jquery

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 + "'>
like image 605
AmazingDayToday Avatar asked Jun 23 '26 22:06

AmazingDayToday


2 Answers

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.

like image 142
TeaCoder Avatar answered Jun 26 '26 11:06

TeaCoder


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);
}
like image 33
Brad Evans Avatar answered Jun 26 '26 10:06

Brad Evans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!