Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select within a container with jQuery?

Tags:

jquery

dom

<div id="container1">
<span>...</span>
</div>
<div id="container2">
<span>...</span>
</div>

Say if I have get the jQuery object $('container1'),how to find the <span> in it?

like image 826
omg Avatar asked Sep 11 '09 06:09

omg


People also ask

How do I select something in jQuery?

The select() method is an inbuilt method in jQuery which is used when some letters or words are selected (or marked) in a text area or a text field. Syntax: $(selector). select(function);

How do I find the element inside a div?

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.

How do I select a class inside a div?

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 .

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. var myDomElement = document. getElementById( "foo" ); // A plain DOM element.


1 Answers

I know you have accepted an answer, I'd just like to add another way of doing this:

$("span", $container1); //This will start in your variable $container1
                          and then look for all spans

I haven't tested performance on these yet, so I don't know which is better. Just thought I'd let you know you have more options (:

like image 64
peirix Avatar answered Oct 25 '22 11:10

peirix