Say I have a map on an array of elements. The callback function takes the index and the value at that position in the array.
If I wrap the array element that the callback receives in $(), it behaves as I expect. If I use it without wrapping it in $(), it gives an error.
var nonHiddenElements = $( "form :input" ).not(':hidden');
nonHiddenElements.map(function(index, element){
input_id = $(element).attr('id'); // this works
input_id = element.attr('id') ; // this gives an error
})
Can someone explain how this works. Is this a jQuery quirk, or a JavScript thing?
What type of objects does my nonHiddenElements array contain exactly? What is element that gets passed to the callback? And mainly what is the $() doing?
.attr()is a jquery keyword. In order to use that you need to call jquery by using$(element).$(element)returns ajQueryobject that wraps theDOM element. api.jquery.com/jquery