3

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
1

1 Answer 1

2

Try

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

See this fiddle

answered Jan 21, 2012 at 19:14
Sign up to request clarification or add additional context in comments.

2 Comments

Not working. I need the append code to appear if there is no other element inside #dis beside h3 Thanks
@cicakman. editted - tested on jsfiddle - workd for me. Any better

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.