I am pretty new to jQuery but I am trying to get a code setup which hides a div when the ‘innerHTML’ is null. I tried using the code below. But it doesn’t work! Where is my fault??
if (($("#php-errors").html).length) {
$("#php-errors").css("display", "block");
}
else {
$("#php-errors").css("display", "none");
}
One line, and using
.show()and.hide()methods:Using the ternary operator that says:
DEMO JSFIDDLE
A good practice would be to cache your element inside a var, let’s call it
var $el, and use it like:Much more readable, and
it will save you some micro processing time 😉 but it really helps in terms of cross-function reusability (if defined outside the function.)