let's say I have a markup like this:
<div id="foo"> ... <span id="moo"> ... </span> ... </div>
and I want to select #moo.
why $('#foo').find('span')
works, but $('span', $('#foo'));
doesn't ?
You can use the children property of getElementById() method in JavaScript to get or extract unique ids of all the DIV elements inside a DIV 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. var myDomElement = document. getElementById( "foo" ); // A plain DOM element.
Use document.querySelector on to select a div and then select an element within it with the given class after that. We just call querySelector on the element with the ID mydiv to select items inside that div. Therefore, innerDiv is the div with the class myclass .
Description: Selects all elements with the given tag name.
You can use any one these [starting from the fastest]
$("#moo") > $("#foo #moo") > $("div#foo span#moo") > $("#foo span") > $("#foo > #moo")
Actually, $('#id', this); would select #id at any descendant level, not just the immediate child. Try this instead:
$(this).children('#id');
or
$("#foo > #moo")
or
$("#foo > span")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With