tags:

views:

446

answers:

2

Hello, 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

+2  A: 

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");
});
Guffa
Thank you! I didn't know that, and it works
Santiago
A: 

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")
elwyn