Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery check if ANY element exists

Tags:

jquery

I have this code which is not working jQuery

if($("#dis(h3)").length == 0) $("#dis").append("<p>no display</p>");

HTML:

<div id="dis">
<h3>Title</h3>
//append here
</div>

The append code should work

<div id="dis">
<h3>Title</h3>
<p>Text</p>
</div>

The append code should not work.

Edit: I need the append code to appear if there is no other element inside #dis beside h3

What should i do to make it as above(intended)

like image 447
cicakman Avatar asked Dec 06 '25 12:12

cicakman


1 Answers

Try

if($("#dis > h3").siblings().length == 0) $("#dis > h3").append("<p>no display</p>");

See this fiddle

like image 109
Crab Bucket Avatar answered Dec 09 '25 03:12

Crab Bucket



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!