0

I have this code

$.each($('input:checked', '#components-holder'), function(index, input){
    console.log(input.attr('value'));
});

And I got this error:

undefined is not a function

How can I iterate on all radio in my page and got value ?

Jonas
  • 121,568
  • 97
  • 310
  • 388
Thomas Shelby
  • 1,340
  • 3
  • 20
  • 39

1 Answers1

1

The object sent to your callback as input is not a jQuery object, so you can't use jQuery methods. You need to convert it into a jQuery object to use jQuery methods:

console.log($(input).attr('value'));

Or use a native DOM property:

console.log(input.value);

Or, you may wish to use map to get the appropriate values:

var values = $('#components-holder input:checked').map(function(index, input) {
    return input.value;
}).get();

values is now an array containing all the relevant values.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318
  • It works, but when I can use input.data('price') instead of input.value it doesn't work – Thomas Shelby Jan 24 '15 at 18:39
  • 1
    @PawełBrzoski Again, it is a DOM object, not a jQuery object. `data` is a jQuery method, so you need a jQuery object. `$(input).data('price')` will work. – lonesomeday Jan 24 '15 at 18:57