Skip to content

Files

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Latest commit

Sep 2, 2018
4e14d21 · Sep 2, 2018

History

History
33 lines (21 loc) · 876 Bytes

how-do-i-select-elements-when-i-already-have-a-dom-element.md

File metadata and controls

33 lines (21 loc) · 876 Bytes
<script>{ "title": "How do I select elements when I already have a DOM element?" }</script>

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.

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:

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

Unfortunately, you cannot concatenate strings to objects.

Related Articles

so first pass in the obj then use find.

  var jMyDomElement =  $( myDomElement);
  
  jMyDomElement.find(".bar")
  
  jMyDomElement.find("a")
``