views:

212

answers:

4

I'm looping through cells in a table row. each cell has a text box in it, and I want to take the value of the text box and push it onto an array.

function dothing() {
        var tds = $('#'+selected+' td');
        var submitvals = new Array();
        tds.each(function(i) {
            var val = $(this).children('input')[0].val();
            submitvals.push(val);
        });
    }

Theres more to the function, but this is all that is relevant. For some reason, when i run this code, i get HTMLInputElement has no method 'val'. I thought that input elements were supposed to have a val() method in jquery that got the value so this makes no sense. Am I missing something/doing it wrong?

+3  A: 

val() is a jQuery method. .value is the DOM Element's property. Use [0].value or .eq(0).val()....

meder
+1  A: 
function dothing() {
    var tds = $('#'+selected+' td');
    var submitvals = new Array();
    tds.each(function(i) {
        var val = $($(this).children('input')[0]).val();
        submitvals.push(val);
    });
}
edtsech
a little overkill to extract the raw element and re-jQuerize it ..
Gaby
yes, agree with you, but i wanted to show that val() method it's method of $(), non a DOM element.
edtsech
+1  A: 

.val() is a jquery method. Using [0] returns the DOM element, not the jquery element

var val = $(this).children('input:first').val();
Ben Rowe
+2  A: 

.val() is a jQuery function, not a javascript function. Therefore, change:

var val = $(this).children('input')[0].val()

To:

var val = $(this).children('input:eq(0)').val()
Alex