Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if div contains an element in jQuery

I'm bulding some drag and drop widgets in jQuery, once they've been dropped I need to check if my drag and droppable widget are inside another div.

<div id="droptarget">
    <div class="widget">I'm a widget!</div>
</div>

I've had a look at $('#droptarget').each but can't seem to figure it out. Any ideas?

like image 999
Tom Avatar asked May 21 '09 10:05

Tom


People also ask

How do I check if a div contains an element?

To check if a div element contains specific text:Use the textContent property on the element to get the text content of the element and its descendants. Use the includes() method to check if the specific text is contained in the div . If it is, the includes() method returns true , otherwise false is returned.

How do you check if a div contains a class in jQuery?

jQuery hasClass() Method The hasClass() method checks if any of the selected elements have a specified class name. If ANY of the selected elements has the specified class name, this method will return "true".

How do you check if an element exists or not in jQuery?

In jQuery, you can use the . length property to check if an element exists. if the element exists, the length property will return the total number of the matched elements. To check if an element which has an id of “div1” exists.

How use contains in jQuery?

jQuery :contains() SelectorThe :contains() selector selects elements containing the specified string. The string can be contained directly in the element as text, or in a child element. This is mostly used together with another selector to select the elements containing the text in a group (like in the example above).


1 Answers

If you want to select the outer div:

$("#droptarget:has(div.widget)")

If you want to select the widget:

$("#droptarget > div.widget")
like image 134
cletus Avatar answered Oct 03 '22 11:10

cletus