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;
    });
Share:
84,903

Related videos on Youtube

JimmyBanks
Author by

JimmyBanks

Hey

Updated on July 27, 2022

Comments

  • JimmyBanks
    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
    Admin about 12 years
    .each() offers a second parameter element which is an alternative to this
  • Kyle Macey
    Kyle Macey about 12 years
    @AndreasNiedermair which would probably save one lookup and microincrease performance. Good call, mate
  • Admin
    Admin about 12 years
    you 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
    andres.gtz over 8 years
    I 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
    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
    andres.gtz over 8 years
    thank you for the explanation, is all clear now! Cheers!
  • Kivylius
    Kivylius over 8 years
    why the need of the .get() ? its a AJAX function.
  • Compaq LE2202x
    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
    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
    CodeGuru about 6 years
    How do you set value instead of get