<script>
$(document).ready(function(){
$("#coiso").addClass("four");
});
</script>
<div id="coiso">1</div>
<div id="coiso">1</div>
<div id="coiso">1</div>
<div id="coiso">1</div>
<div id="coiso">1</div>
Asked
Active
Viewed 725 times
1

joews
- 29,767
- 10
- 79
- 91

costapombo
- 43
- 9
-
10Your divs have no names, they have [`id`](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes#id)s, which should be unique within a document. Use [`class`](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes#class) attribute to group HTML elements. – Teemu Nov 30 '13 at 22:24
-
[Fixed demo](http://jsfiddle.net/pC2Bj/150/) – gdoron Nov 30 '13 at 22:27
-
If you really need to use id's you could use :first selector like I used http://jsfiddle.net/8Xgbd/ but classes are way better in this case – Bas van Dijk Nov 30 '13 at 22:51
2 Answers
0
"id" is supposed to be unique. This is how I had solved a similar problem.
<script>
$(document).ready(function(){
$("div[data-id]").addClass("four")
});
</script>
<div id="coiso" data-id="">1</div>
<div id="coiso1" data-id="">2</div>

ragche
- 443
- 3
- 11
0
That's right, an id is indeed supposed to be unique. That's a bad pratice, I advice you to respect the standard.
With a class, in JavaScript, you can do the same thing with something like this :
var Elements = document.getElementsByClassName('NameClass');
With jQuery, I don't know if a similar method exist. If it's the case, you'll find it in his doc on his website.

Wagner_SOFC
- 117
- 4