Reputation: 117
I'm trying to get an input id from an array, but not sure if I am getting the data correctly or whats going wrong, because I need to get the id attribute to make some operations. First of all, I create dynamically this:
$('<input>').attr({
type : 'hidden',
name : 'id_products[]',
value : product_id,
id : 'id_'+product_id,
readonly : 'readonly'
}).appendTo('#form);
Then, I get the array in this way:
var selectedIDs = $('[name="id_products[]"]');
After that, if the length of selectedIDs is greater than 0, I use a for cycle to get the elements. But I need the id attribute (or the value of the input) from the current element:
if(selectedIDs.length > 0) {
//There's at least 1 selected product
for(var j=0; j < selectedIDs.length; j++)
{
var id_temp = selectedIDs[j].val();//.attr("id");
...
}
...
}
But firebug says: [object HTMLInputElement].val() is not a function (obviously the same with attr()). Any ideas?
Upvotes: 0
Views: 1171
Reputation:
It should be $(selectedIDs[j]).val()
or selectedIDs[j].value
JQuery function on native DOM Element won't work
Upvotes: 1