id = [<input type="hidden" name="id" value="test_id">, <input type="hidden" name="id" value="test_id">]
id.val()
Result: "test_id"
I have an “id object” with two HTML Elments. There are the same because I will update two identical Elements with one ajax get.
I got this variable “id” with a selector who search for a form.
My Question:
Is .val() checking both Elements or only the first Element in this object “id”?
And what is if one of the Elements has a different value.
What I did after this question!
- I’m using now .map() to get both values from the objct
- Added a check for the equality of the id’s.
- Learned not to skip the first big sentence (Description) on the
Jquery Docs =)
Thanks a lot!
.val()will check the value of the first element in the set of elements it is run on. If you pass.val()a function like this:.val(function (index, value) {})then you can get the value for every element in the set of element that the function is run on.You might want to check-out the docs for
.val(): http://api.jquery.com/valHere is a demo: http://jsfiddle.net/mV4U6/2/ (If you change the value of any input then the value of all the inputs will be
console.loged)As lonesomeday stated, this requires you to return the value so the form element will retain its value. You can however use
.map()to get the values of each input:Here is a demo using
.map(): http://jsfiddle.net/mV4U6/3/…Also possible with a loop: