jQuery get the input value in an .each loop
84,903
Solution 1
You can use this
to access the current element in the loop:
valu = $(this).val();
The current element is also sent as a parameter to the callback function, so you can pick it up:
.each(function(index, elem) {
Then use the parameter:
valu = $(elem).val();
Solution 2
$('.custemb, input[name=cb], input[class=multadd]').live("click", function() {
$('input[class=multadd]:checked').each(function(index) {
var $this = $(this);
val = index + 2;
valu = $this.val();
multiz = multiz + '&aid' + val + '=' + valu;
});
});
Solution 3
var texts= $(".class_name").map(function() {
return $(this).val();
}).get();
Solution 4
Use this
to find the control that was clicked
$('input[class=multadd]:checked').each(function(index) {
val = index + 2;
valu = $(this).val();
multiz = multiz + '&aid' + val + '=' + valu;
});
Related videos on Youtube
![JimmyBanks](https://i.stack.imgur.com/vHHkU.jpg?s=256&g=1)
Comments
-
JimmyBanks almost 2 years
I am trying to get the input value in an each loop of a checkbox, i cant figure out how to get this to work, the value keeps outputting as the first checkbox value.
$('.custemb, input[name=cb], input[class=multadd]').live("click", function() { $('input[class=multadd]:checked').each(function(index) { val = index + 2; valu = $('input[class=multadd]:checked').val(); multiz = multiz + '&aid' + val + '=' + valu; }); });
the problem is the output of the variable
valu
is the first checkbox of the overall each loop, not the current checkbox of the loop, i need the current value.Any ideas?
-
Admin about 12 years
.each()
offers a second parameterelement
which is an alternative tothis
-
Kyle Macey about 12 years@AndreasNiedermair which would probably save one lookup and microincrease performance. Good call, mate
-
Admin about 12 yearsyou didn't get it ... using the second parameter you can (in other scenarious) extract the method for the each-iteration and therefore maybe create a magically reusage scenario ...
-
andres.gtz over 8 yearsI would like to ask why do I have to use the selector on "elem" to access to the value if "elem" is already the input element? Thanks
-
Guffa over 8 years@mkmnstr: Because
elem
is the element, not a jQuery object for the element. The$()
function takes different kinds of parameters, not only selectors. If you send in an element it will simply create a jQuery object containing that element. The call$(this)
in the first example uses the function in the same way. -
andres.gtz over 8 yearsthank you for the explanation, is all clear now! Cheers!
-
Kivylius over 8 yearswhy the need of the .get() ? its a AJAX function.
-
Compaq LE2202x about 8 years@Guffa What if one of the elements is a radio, using
.each(function(index, elem) {
how do I get the selected radio using elem object? -
Guffa about 8 years@CompaqLE2202x: You can use
$(elem).is(':radio')
to check if the element is a radio button, and$(elem).is(':checked')
to check if it is selected. You can combine them as$(elem).is(':radio:checked')
. -
CodeGuru about 6 yearsHow do you set value instead of get