How do I select elements when I already have a DOM element?

jQuery

How do I select elements when I already have a DOM element?

If you have a variable containing a DOM element, and want to select elements related to that DOM element, simply wrap it in a jQuery object.

1
2
3
                          
var myDomElement = document.getElementById("foo"); // a plain DOM element
$( myDomElement ).find("a"); // finds all anchors inside the DOM element

Many people try to concatenate a DOM element or jQuery object with a CSS selector, like so:

1
                          
$( myDomElement + ".bar" ); // This is equivalent to $("[object HTMLElement].bar")

Unfortunately, you cannot concatenate strings to objects.

Related Articles