Jquery: Add rel attribute to <a> tags within all <li> tags of a certain class

Saahir Foux picture Saahir Foux · Apr 23, 2011 · Viewed 25.6k times · Source

I'm trying to add a rel="lightframe" attribute to all my 'edit' links within my admin_links_node_edit class.

<li class="admin_links_node_edit">
<a href="[link]" title="Edit">Edit</a>
</li>

My code so far looks like this:

$('.admin_links_node_edit a').each(function() {
        $(this).attr('rel','lightframe'); 
});

Answer

Vijay Dev picture Vijay Dev · Apr 23, 2011

You don't need to use each(). jQuery's selectors will do it for you :)

$('.admin_links_node_edit a').attr('rel', 'lightframe')

The above code will do the trick.