Reputation: 61
I have
<div id="exp">
<input type="hidden" name="sum[]" value="1" />
<input type="hidden" name="sum[]" value="2" />
<input type="hidden" name="sum[]" value="3" />
<input type="hidden" name="sum[]" value="4" />
</div>
And I want in jquery get the value of the last element of the array. I've tried:
alert(sum[sum.length-1]);
But shows me 'undefined'. What am I doing wrong?
Upvotes: 1
Views: 3363
Reputation: 707396
You can use this:
var lastValue = $("#exp input:last").val();
This uses a jQuery selector to find the last input
element that is a child of #exp
and get its value.
You can see it work here: http://jsfiddle.net/jfriend00/Lh7sJ/
Upvotes: 6