Getting multiple elements' text with Jquery
Yes, you can :).
var tableVals= {}
$('#projects td').each(function() {
tableVals[$(this).attr('id')] = $(this).text();
});
Please, remember to use object instead of array if your keys are not numeric.
You can do something like below,
var tableVal= [];
$('#projects tr:eq(0) td').each (function () {
tableVal[this.id] = $(this).text();
});
Note: :eq(0) - means 1st row.. Modify accordingly if you want to do for all rows or let me know if you need help with that.
If you actually want an Array, use .map()
with .toArray()
.
var tableVal = $('#projects td').map(function(i,v) {
return $(this).text();
}).toArray();
Otherwise if you're actually going to use non numeric indices, you want an Object, using the techniques in the other answers.
First, give your tr
an id
:
<tr id="rowToGetDataFrom">
Then you can get the array you want like this:
var tableVal = $('#rowToGetDataFrom td').map(function () { return $(this).text(); });
Demo: http://jsfiddle.net/alecgorge/3ApnB/