Toggle show/hide divs if data.entities.length > 0, tertiary operator doesn't work on Javascript/jQuery? -
i need toggle visible/hidden 2 different divs if data.entities.length > 0
, i'm doing:
if (data.entities.length > 0) { var toggle = data.entities.length ? true : false; // if condition true show otherwise hides $('#resultadonorma').toggle(toggle); // reversal process // if condition true goes hide otherwise goes show $("#sinresultadosbuscarnormas").toggle(!toggle); }
but it's not working since none div show/hide not matter happen condition, wrong? can use tertiary operator in javascript?
your use of conditional operator inside if()
statement, succeeds if there's positive .length
, toggle
true
.
you should remove if()
statement.
var toggle = data.entities.length ? true : false; $('#resultadonorma').toggle(toggle); $("#sinresultadosbuscarnormas").toggle(!toggle);
or rid of conditional, , pass .length
directly. idea coerce boolean though.
$('#resultadonorma').toggle(!!data.entities.length); $("#sinresultadosbuscarnormas").toggle(!data.entities.length);
Comments
Post a Comment