Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Attribute Selectors OR Operation

Here is my html:

<div id="l">l</div>
<div id="a">a</div>
<div id="i">i</div>

How to change the color of only the l and a attributed elements? I searched for a selector with OR, something like this:

$(function(){
    $('div[id=l,a]').css('color','red');
});

It's not working, is there something like that in jQuery?

EDIT Thank you guys it's working now:

$('[id=l], [id=a]').css('color', 'red');

But what if I want to search for those IDs inside a <div> like $('[id=l], [id=a]','div'). This doesn't work, how should I do it?

like image 927
trrrrrrm Avatar asked Jun 27 '11 19:06

trrrrrrm


People also ask

How do you select an element with specific attributes?

The [attribute^="value"] selector is used to select elements with the specified attribute, whose value starts with the specified value. The following example selects all elements with a class attribute value that starts with "top": Note: The value does not have to be a whole word!

How do you check if an element has an attribute in jQuery?

Using jQuery The idea is to use the . attr() method, which returns the attribute's value for an element if it is present and returns undefined if the attribute doesn't exist.


1 Answers

$(function(){
    $('#l, #a').css('color','red');
});

Fiddle: http://jsfiddle.net/maniator/2Xuat/

like image 118
Naftali Avatar answered Oct 18 '22 01:10

Naftali