valerio0999 valerio0999 - 10 months ago 44
HTML Question

jquery has in if statement triggering in any case

I'm using jquery

to check if a
has an
inside it. it's in a
statement but it fires in any case, even if the
doesn't have the
inside it:

if ($(".cs-error").has("ul")) {
alert("it has a UL")

<script src=""></script>
<div class="cs-error">


what am i missing? I didn't try much because well.. I'm supposed to do this simple thing and I couldn't find a better way to do it. I already use .has() in other circumstances and it always worked

Answer Source

$(".cs-error").has("ul") will give you a jQuery object nonetheless - but an empty one.

So you should check if there was at least one element found:

if ($(".cs-error").has("ul").length) {