phantom phantom - 6 months ago 16
jQuery Question

writing if/else statement as ternary operator

How would I write this using a ternary operator?



if (!$('#privacy_check').is(':checked')) {
$('#privacy_check').css('outline-color', 'red');
$('#privacy_check').css('outline-style', 'solid');
$('#privacy_check').css('outline-width', 'thin');
} else {
$('#privacy_check').css('outline-color', 'none');
$('#privacy_check').css('outline-style', 'none');
$('#privacy_check').css('outline-width', '0');
}





I have tried



!$('#privacy_check').is(':checked') ? $('#privacy_check').css('outline-color', 'red'); $('#privacy_check').css('outline-style', 'solid');$('#privacy_check').css('outline-width', 'thin') :
$('#privacy_check').css('outline-color', 'none');$('#privacy_check').css('outline-style', 'none');$('#privacy_check').css('outline-width', '0');




Answer
var $elem = $('#privacy_check');
$elem.css($elem.is(':checked') ?
    { outlineColor: 'none', outlineStyle: 'none', outlineWidth: 0 } :
    { outlineColor: 'red', outlineStyle: 'solid', outlineWidth: 'thin' })
Comments