SeattleAls SeattleAls - 7 months ago 15
Javascript Question

True-False condition inline on HTML element (checkbox)

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 + "'>

Answer

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);
}
Comments