Tomy Racks
Tomy Racks

Reputation: 21

add class for equivalent other ID

<select id="list" multiple=multiple>
    <option value="1" id="one" selected="selected">one </option>
    <option value="2" id="two" selected="selected">two </option>
    <option value="3" id="three">three </option>
</select>

<ul id="uli">
    <li aaa="one">one</li>
    <li aaa="two">two</li>
    <li aaa="three">three</li>
</ul>

With the CSS:

.back {
 color: red;
}

And the Javascript:

$("#list").find("option:selected").each(function() {
    // logic here
});

Into the each() callback, I want to add class for equivalent in uli. I would like to use addClass(.back) for li where attr aaa == id for list.

live: http://jsfiddle.net/yjL9n/3/

Upvotes: 1

Views: 405

Answers (4)

andyb
andyb

Reputation: 43823

$('li[aaa=' + this.id + ']').addClass('back');

$('table').find('td[aaa="'+this.id+'"]').addClass('back');

added to the inner part of your fiddle will do it. Updated based on comment from OP.

Upvotes: 1

Jacek Kaniuk
Jacek Kaniuk

Reputation: 5229

Here you go:

$("#uli").find('[aaa="'+this.id+'"]')
    .addClass('back');

http://jsfiddle.net/yjL9n/4/

Upvotes: 7

Richard Dalton
Richard Dalton

Reputation: 35793

This works (and is tidier):

$("#list option:selected").each(function() { 
    $('#uli li[aaa="' + this.id + '"]').addClass('back');
});

http://jsfiddle.net/yjL9n/5/

However I wouldn't use aaa as an attribute!

Upvotes: 2

ShankarSangoli
ShankarSangoli

Reputation: 69905

Try this

$("#list").find("option:selected").each(function() {
   $("#uli").find('[aaa="'+this.id+'"]').addClass('back');
});

Upvotes: 1

Related Questions