lets say I wanted to search through divs with a certain class name.and if the divs dont have a certain id I want to add a class to them. ex.
<div id ="1" class="head"></div>
<div id ="2" class="head"></div>
<div id ="3" class="head"></div>
if(div.id != "1")
{
add class to all divs not = 1
}
how would this work thank you
jQuery
You can use this code to do that…
jsFiddle.
For better performance, use…
jsFiddle.
Without jQuery
For modern browsers…
jsFiddle.
You could drop the
filter()and use:not(), like Zeta’s answer.For our lovable older IEs…
jsFiddle.
Depending on how far you need to support, you could always use
document.getElementsByClassName()so long as you know you will be required to filterdivelements if other elements may contain the same class name and you must havedivexclusively.Additionally, an
idattribute can not start with a number, according to the spec.