1

I am trying to get a list of values within a div that I will format later using the .each() method. They are input values which are hidden my html and jquery call looks like this.

<div id="container_0">
   <input type="hidden" id="check_data" value=10>
   <input type="hidden" id="check_data" value=20>
</div>

Jquery:

var list = $('#container_0 input#check_data');

$(list).each(
      function() {
           alert($(this).val());
       }
);

This however is not returning any values. Any help would be greatly appreciated.

1
  • your values (10, 20), should be in quotes and the input elements should be properly closed. Proper XHTML always makes things work better (though in this case I doubt improper html code is causing the problem) Commented Mar 30, 2010 at 17:42

2 Answers 2

8

You can't use the same ID multiple times, this is invalid HTML. When you do this, any results will be unpredictable, especially across browsers.

This should work to alert the values though:

$('#container_0 input').each(function() {
   alert($(this).val());
});

If you used a class, like this:

<div id="container_0">
   <input type="hidden" class="check_data" value=10>
   <input type="hidden" class="check_data" value=20>
</div>

Then this would find only those inputs:

$('#container_0 input.check_data').each(function() {
   alert($(this).val());
});
Sign up to request clarification or add additional context in comments.

1 Comment

@kodisha - Definitely, was just adding that option :)
0

Nick is completly right, you are not allowed to use id's more than once within one html document.

What could help you is: choose different id's for your inputs

Then select the two inputs by something like:

 $('#container_0 input')

In case that matches too many elements, you still can add classes to the inputs:

<div id="container_0">
   <input class="fencyInput" />
   <input class="fencyInput" />
   <input class="notFancy" />
</div>

Where a selector like

 $('#container_0 input.fencyInput')

would only match the first two divs (classes can be used more than once).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.