0

I want to check one html element exist inside another html element using jquery.Parent element have id 'preview' and child element have id 'draggable'.How check child element present inside parent element?

My code is

     if($("#preview").has("#draggable").length)
      {
          alert( "First element contain" );
      }

Any body give any solution for these issue?

3
  • 1
    as you are using id for child element then you can directly check if($("#draggable").length > 0) provided that id must be unique through out the html document. Commented Dec 17, 2014 at 6:21
  • I believe you yourself provided answer. Does it not working for you ? Show structure of both element than we will able to tell you Commented Dec 17, 2014 at 6:24
  • I want to check id 'draggable' present inside 'preview' or not?it is a draggable element.Some time it is inside preview or not Commented Dec 17, 2014 at 6:26

5 Answers 5

4
alert($("#preview #draggable").length) ;

This will give you non zero length if there is a #draggable is contained inside #preview.

Sign up to request clarification or add additional context in comments.

Comments

0

you can use find() function to find inner element <selector>

if($("#preview").find("#draggable").length){
    alert( "First element contain" );
}

Comments

0

This is assuming that you are looking to the immediate children of the #preview selector.

if($("#preview").children('#draggable').length > 0){
    console.log( "First element contain" );
} else {
    console.log('none');
}

If you have to traverse to the inner elements (e.g. possibility that #draggable is inside other elements), then proceed to @Affan 's asnwer. :)

Comments

0

Try this in the if condition

 $('#preview').find('#foo').length

Thank You

Comments

0

$("#preview").has("#draggable") it returns the boolean true or false.

if($("#preview").has("#draggable")){


}

1 Comment

@shkk No in first condition it is false it doesn't use

Your Answer

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.