item1
item...">

Select all html elements based on rel value

I have some html elements in my code like this

<div rel="test1">item1</div>
<div rel="test1">item2</div>
<div rel="test1">item3</div>
<div rel="test2">item4</div>
<div rel="test2">item5</div>

and I need a way to select all the divs that use rel="test1"and add a class to them, how can I do this with jQuery?

+5
source share
4 answers
$('div[rel=\'test1\']')

http://api.jquery.com/category/selectors/attribute-selectors/

Then you can add the class with .addClass(). http://api.jquery.com/addClass/

+6
source
​$('div[rel="test1"]')​.addClass("myClass");​

Demo

+4
source
$('div[rel="test1"]').addClass('fooClass');

Live demo

+1
source
$(function(){
    $("div[rel='test1']").addClass("newClass");       
});

working sample http://jsfiddle.net/4WEBk/13/

+1
source

All Articles