tags:

views:

52

answers:

4

Hi guys,

I have a quick question that I need some help with if possible. Am I able to have more than one input with the same id and loop through the values of these? Something similar to the code below:

<p>ID:<input name="check_id" type="hidden" id="check_id"  value="1"/></p>
        <p>ID:<input name="check_id" type="hidden" id="check_id"  value="2"/></p>

jQuery("#check_id").each(function(){
var check_data = jQuery(this).val();
alert(check_data); 
});

Thanks

Paul

+1  A: 

No. IDs must be unique. You will likely only select the first.

You could possibly do something like this, though it is less efficient:

jQuery("input[id=check_id]").each(function(){
    var check_data = jQuery(this).val();
    alert(check_data); 
});

But if you're going to do that, you might as well fix your IDs to be unique, and select by the name attribute.

jQuery("input[name=check_id]").each(function(){
    var check_data = jQuery(this).val();
    alert(check_data); 
});

If all the <input> elements you want are contained in a container, you could speed things up by specifying that container.

jQuery("#myContainer input[name=check_id]").each(function(){
    var check_data = jQuery(this).val();
    alert(check_data); 
});

This code example presumes there is a common ancestor with the ID myContainer.


EDIT: With regard to your question in the comment below, return false; inside an .each() loop only exits the loop. You could however set a flag that will tell the subsequent code whether or not to execute.

Something like this:

var shouldContinue = true;

jQuery("#myContainer input[name=check_id]").each(function(){
    var check_data = jQuery(this).val();
    if( someCondition ) {
        shouldContinue = false; // signal to not execute subsequent code
        return false;  // Halt the loop
    }
});

if( shouldContinue ) {
     // execute code if shouldContinue is true
}
patrick dw
Thanks for the answer it was exactly what i needed
Paul Atkins
@Paul - You're welcome. :o)
patrick dw
Hi Patrick, is it possible to stop the remainder of my script running using return false inside the loop or does that only stop the remaining loops?
Paul Atkins
@Paul - Inside an `.each()` loop, `return false;` will only stop the loop. You could however set a variable to signal whether the code after the loop should execute. I'll update my answer with an example.
patrick dw
Thank you very much for your help Patrick. That worked great. Paul
Paul Atkins
@Paul - You're very welcome. :o)
patrick dw
A: 

In HTML, ids are unique. So no, you will just get a single object when using the #.. selector. Which one that is when you have assigned a single id multiple times is not defined; actually you should fix that!

poke
A: 

The whole point of the HTML id attribute is that it is supposed to be unique.

Perhaps you want to use classes instead? For example, consider the following:

<p>ID:<input name="check_id" type="hidden" class="check_id" value="1" /></p>
<p>ID:<input name="check_id" type="hidden" class="check_id" value="2" /></p>

...

jQuery(".check_id").each(function() {
    var check_data = jQuery(this).val();
    alert(check_data); 
});
Cameron
A: 

You can remove the id attribute (which as others have said, must be unique in a document) and use a attribute-equals selector to select by name, like this:

jQuery("input[name=check_id]").each(function() {
  var check_data = jQuery(this).val();
  alert(check_data); 
});

Or add class="check_id" (still removing the id="check_id") and use a class selector, like this:

jQuery(".check_id").each(function() {
  var check_data = jQuery(this).val();
  alert(check_data); 
});
Nick Craver
I wonder if it is quicker in IE to select using the attribute-equals selector, since it doesn't need to split apart the potential multiple class names. Any thoughts?
patrick dw
Thanks that is exactly what I was trying to do. Thank you for taking the time to reply
Paul Atkins
@patrick - Possibly, but the `className` is on every element, whereas `name` is undefined and jumps out early, so I'm not sure, would have test it out, though testing in older IE scares me :)
Nick Craver
Nick - Good point. Maybe I'll do a little testing one of these days. I'll leave a note here if I get around to it. :o)
patrick dw