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
?
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
?
4 Answers
Reset to default 6$('div[rel=\'test1\']')
http://api.jquery./category/selectors/attribute-selectors/
You can then add a class with .addClass()
. http://api.jquery./addClass/
$('div[rel="test1"]').addClass("myClass");
Demo
$('div[rel="test1"]').addClass('fooClass');
Live DEMO
$(function(){
$("div[rel='test1']").addClass("newClass");
});
working sample http://jsfiddle/4WEBk/13/