tags:

views:

49

answers:

1

Hello,

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

+5  A: 

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"

Paolo Bergantino
that's exactly it, or if you just want elements within the `ul` encase you have several `ul` within myElement then drop a `>` in there like so: `$('#myElement > a.myElementLink');`
RobertPitt
@Robert, that wouldn't work because the *li* elements are the immediate children of the *ul* element
Andy E
I missed your second part here (edited and added after the fact?) which was the same as my answer. Which I will be deleting. Using the context selector ('thing', 'thing') is a better habit, in my opinion, than just making a more specific CSS selector like in your first example, since the second argument can take the JavaScript "this".
Steve Paulo
@Paolo: Perfect!!! Thanks!!!
Lorenzo
yea sorry, forgot to ad the ul into the selector, and +1 for the second param.
RobertPitt