Mohammad Naji
Mohammad Naji

Reputation: 5442

jQuery: Selecting specific input hidden element (by index) in an array of input hiddens

I have the following HTML:

<table>
<tr><td>...</td><td><input type="hidden" name="price[1]" value="10"></td><td>...</td>
<tr><td>...</td><td><input type="hidden" name="price[6]" value="230"></td><td>...</td>
<tr><td>...</td><td><input type="hidden" name="price[7]" value="40"></td><td>...</td>
<tr><td>...</td><td><input type="hidden" name="price[10]" value="10"></td><td>...</td>
</table>

I have also got that this is important to say that they are stored in table tds and it's preferred not to bring them out of the table. (So I updated the code to be more like in real)

What I want, is a way to select one of them by their index() number to be able to get their val().

I know that their index is counted from 0 to 3. I want to get the val() of the second one with index(1).

Upvotes: 1

Views: 2712

Answers (4)

Barmar
Barmar

Reputation: 781340

To get the value of the second hidden input:

$('input[type=hidden]:eq(1)').val()

Upvotes: 4

4b0
4b0

Reputation: 22323

try:

fiddle

var a="";
$("input[type=hidden]" ).each(function( index ) {
    a+=($(this).val()) + ",";

});
alert(a);

Upvotes: 1

yeyene
yeyene

Reputation: 7380

DEMO http://jsfiddle.net/yeyene/yGCP2/

$(document).ready(function(){
    $('input[type=hidden]').each(function(){
        alert($(this).val());
    });
});

Upvotes: 1

mkutyba
mkutyba

Reputation: 1427

In jQuery it would be:

$('input[type="hidden"]');

http://jsfiddle.net/mattydsw/32NU8/

Upvotes: 1

Related Questions