20

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?

JimmyBanks
  • 4,178
  • 8
  • 45
  • 72

4 Answers4

52

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();
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 1
    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 – andres.gtz Feb 17 '16 at 20:03
  • 3
    @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. – Guffa Feb 17 '16 at 21:49
  • 1
    thank you for the explanation, is all clear now! Cheers! – andres.gtz Feb 17 '16 at 23:28
  • @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? – Compaq LE2202x Jun 21 '16 at 07:09
  • 1
    @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')`. – Guffa Jun 25 '16 at 17:57
  • How do you set value instead of get – CodeGuru May 22 '18 at 15:48
  • how do you know which input the value is coming from I mean if I just want the value of one the value of the other one separately? – dave Oct 05 '22 at 17:56
  • @dave: The code in the question collects the values from the checked checkboxes. If you want to know if a specific checkbox is checked you can test that directly, for example using `var checked = $('input[name=cb]').is(':checked')`. – Guffa Oct 28 '22 at 10:47
5
$('.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;
    });
});
Kyle Macey
  • 8,074
  • 2
  • 38
  • 78
  • 1
    `.each()` offers a second parameter `element` which is an alternative to `this` –  May 19 '12 at 20:33
  • 1
    @AndreasNiedermair which would probably save one lookup and microincrease performance. Good call, mate – Kyle Macey May 19 '12 at 21:10
  • 1
    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 ... –  May 20 '12 at 09:02
5
var texts= $(".class_name").map(function() {
    return $(this).val();         
}).get();
ivarni
  • 17,658
  • 17
  • 76
  • 92
sadam
  • 51
  • 1
  • 1
3

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;
    });
web_bod
  • 5,728
  • 1
  • 17
  • 25