Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 393
themhz Avatar asked Apr 25 '12 16:04

themhz


2 Answers

$('div[rel=\'test1\']')

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

You can then add a class with .addClass(). http://api.jquery.com/addClass/

like image 84
Brad Avatar answered Sep 30 '22 02:09

Brad


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

Demo

like image 42
The Alpha Avatar answered Sep 30 '22 02:09

The Alpha