Reputation: 175
I have some simple list items with link elements inside.
In the example below, what Javascript function could I use to add class="selected" to the link inside the "coffee" list on page load.
<li class="coffee">
<a href="#"> ..... </a>
</li>
<li class="tea">
<a href="#"> ..... </a>
</li>
<li class="coke">
<a href="#"> ..... </a>
</li>
<li class="beer">
<a href="#"> ..... </a>
</li>
Upvotes: 1
Views: 554
Reputation: 218837
You can chain selector items to target child elements:
$('li.coffee a').addClass('selected');
This targets any a
which is a descendant of any li
with the class coffee
. You can also restrict it to immediate child elements:
$('li.coffee > a').addClass('selected');
Upvotes: 1