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)
asked Jan 21, 2012 at 19:08
cicakman
2,0005 gold badges22 silver badges34 bronze badges
-
jquery selectorsJ. Holmes– J. Holmes2012年01月21日 19:11:37 +00:00Commented Jan 21, 2012 at 19:11
1 Answer 1
Try
if($("#dis > h3").siblings().length == 0) $("#dis > h3").append("<p>no display</p>");
See this fiddle
answered Jan 21, 2012 at 19:14
Crab Bucket
6,2679 gold badges43 silver badges73 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
cicakman
Not working. I need the append code to appear if there is no other element inside #dis beside h3 Thanks
Crab Bucket
@cicakman. editted - tested on jsfiddle - workd for me. Any better
lang-js