tags:

views:

34

answers:

5

Hi, I have this:

$(document).ready(function()
    alert($("td").val());
});

But the alert is empty.

How do I select the values in my table with jQuery? Thank you.

edit: I was asked what for:

I have a website that I need the database data from for a javascript function. I get the data from the database, put it a table and then select the data from the table for usage in the javascript. In the middle of my function I hvae the $("td")...whatever form my function.

A: 

val only works on form fields. You want text:

$(document).ready(function() {

alert($("td").text());

});

However, text is only goingto retrun one result so youll have to use each to get an array:

 $(document).ready(function() {
    var txt = [];
    $("td").each(function(){
       txt[] = $(this).text();
    })
    alert(txt.join(','));

    });

Actually, use map as J-P suggests... its the better answer for getting the vals in an array :-)

prodigitalson
+3  A: 

If you'd like them as a comma-separated string of values:

var vals = $('td').map(function(){
    return $.text([this]);
}).get().join(',');

alert(vals);
J-P
A: 

Try:

alert($("td").text());
Topera
+1  A: 

Table cells don't have values, they have HTML content.

Use:

alert($("td").html().join(','));

Instead.

mkoistinen
This won't work. There's no `.join()` method for Strings.
patrick dw
You're absolutely correct. My bad.
mkoistinen
A: 
$(document).ready(function() {
var values;
$('td').each(function() {
    values += $(this).text() + " ";
});
alert(values);
});
Mark Baijens