21

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?

4 Answers 4

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();
Sign up to request clarification or add additional context in comments.

8 Comments

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
@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.
thank you for the explanation, is all clear now! Cheers!
@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?
@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').
|
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;
    });
});

3 Comments

.each() offers a second parameter element which is an alternative to this
@AndreasNiedermair which would probably save one lookup and microincrease performance. Good call, mate
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 ...
5
var texts= $(".class_name").map(function() {
    return $(this).val();         
}).get();

1 Comment

why the need of the .get() ? its a AJAX function.
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;
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.