attr() on radio return: undefined is not a function

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 ?

Answers


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.


Need Your Help

Does it make sense to use .apply( ) and pass the same instance as context?

javascript function call execution apply

I'm reading Javascript Web Applications, from O'Reilly. At various points in the book, the author uses something along the following:

Visualising a custom map with QGIS and D3.js is harmed by the projection

javascript json d3.js projection qgis

These are 4 polygons I have: http://imgur.com/vsVjxE1 in QGIS. I now wanted to use them with D3.js. I already tried out the things in QGIS generated shapefile malformed when converted to topojson by

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.