Reputation: 2320
I need to obtain all the divs with a given id but the jquery each function only obtain the first one.
Example:
<div id="#historial">some html code</div>
<div id="#historial">some html code</div>
<div id="#historial">some html code</div>
<div id="#historial">some html code</div>
script:
$("#historial").each(function() {
alert("one div");
});
if I pass a anchor o a id + anchor ej $("#lala a") it's works ok.
What's wrong?
BR
Upvotes: 4
Views: 13955
Reputation: 10521
An ID should be unique, there should only be one element on the page with a specific ID.
If you need to group those DIVs, use 'class' instead.
<div class="historial">some html code</div>
<div class="historial">some html code</div>
<div class="historial">some html code</div>
<div class="historial">some html code</div>
So the revised jQuery, to look up each DIV with the class 'historal' would look like this:
$("div.historal").each(function() {
alert($(this).text()); //Prints out the text contained in this DIV
});
Also, a sidenote- the # is used by jQuery, not the HTML markup- eg if you had a DIV like
<div id="historal">stuff</div>
you would find that with jQuery like this:
$("#historal")
Upvotes: 6
Reputation: 700342
You can only use a specific id for one element in a page. Use a class instead:
<div class="historial">some html code</div>
<div class="historial">some html code</div>
<div class="historial">some html code</div>
<div class="historial">some html code</div>
$(".historial").each(function(e) {
alert("one div");
});
Upvotes: 13