I have code in the following style :
<tr id="201461">
<td id="0A" style="cursor:pointer" onClick = "ProcessTextBoxClick()" value="Feb 23 2008">Feb 23 2008</td>
<td id="0B" style="cursor:pointer" onClick = "ProcessTextBoxClick()" value="Feb 25 2008">Feb 25 2008</td>
<td id="0C" style="cursor:pointer" onClick = "ProcessTextBoxClick()" value="Feb 28 2008">Feb 28 2008</td></tr><tr id="201460">
<td id="1A" style="cursor:pointer" onClick = "ProcessTextBoxClick()" value="47">47</td></tr>
I have some JQuery where I am getting the id of each row, and now I want to get each value in each td for each row. How do I do this?
var tbl = document.getElementById("tbl-1");
var numRows = tbl.rows.length;
for (var i = 1; i < numRows; i++) {
var ID = tbl.rows[i].id;
Do you want the value of the
value
attribute, the HTML, or the HTML stripped of markup? The various answers so far have mostly gone with "the HTML", at least one went with "the HTML stripped of markup", but none (so far) has gone with "the value of thevalue
attribute". So:Using jQuery:
The end result is an object with properties for each row, with the property name being the row's
id
value, and the property value being an array of thetd
information.So for instance, to get the array for row
201461
, you can do this:The above uses:
$()
to find the rows in the table.map
(followed byget
) to build the array of values.Off-topic:
td
elements don't have avalue
attribute. You might consider usingdata-*
attributes instead.id
values are likely to cause you trouble. Recommend not havingid
values that start with a digit. Although valid in HTML5, they're not valid in earlier versions of HTML and not valid in CSS. Since jQuery uses CSS selectors, if you're using CSS, I would strongly advise sticking to the CSS rules. (In your case, it's really easy: Just put and
on the front of them or seomthing.)If you want to loop over all
<td>
NOTE: This is jQuery whereas your example is native JavaScript.
in jQuery:
You can check it in work here: http://jsfiddle.net/3kWNh/
You can simply do
Your code does not look like jQuery. Are you sure you aren't using the term jQuery as a synonym to JavaScript? :) If that is the case, I suggest you read this question as well; it will make things a lot more clear for you.
Anyway, here goes JavaScript:
Alternatively, if you really use jQuery:
What you are using there is not jQuery, if you are using jQuery you can use
.html();
to retrieve the value.Here is your code with jQuery: