Question

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?

Was it helpful?

Solution

It should be $(selectedIDs[j]).val() or selectedIDs[j].value

JQuery function on native DOM Element won't work

http://jsfiddle.net/N2HAS/1/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top