Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select all element of the same class within a div with jQuery?

Tags:

jquery

I have this markup:

<div id="myElement">
<ul>
    <li><a href="#1" class="myElementLink">Link 1</a></li>
    <li><a href="#2" class="myElementLink">Link 2</a></li>
    <li><a href="#3" class="myElementLink">Link 3</a></li>
</ul>
</div>

Which is the syntax to select all "myElementLink" within "myElement"?

Thanks for helping

like image 434
Lorenzo Avatar asked Sep 10 '10 22:09

Lorenzo


People also ask

How do you select all elements inside a div element?

Use the element element selector to select all elements inside another 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 .


1 Answers

The magical $() function of jQuery can take pretty much anything you would do in regular CSS to style a particular set of elements. In your case, this would do the trick:

$('#myElement a.myElementLink');

Specifying which elements will have a particular class (ie, a.class vs .class) is much faster if possible.

You could also pass the second argument, known as the context to the jQuery function to come up with:

$('a.myElementLink', '#myElement');

Which basically says "search for the 1st argument within the 2nd argument"

like image 65
Paolo Bergantino Avatar answered Sep 21 '22 18:09

Paolo Bergantino