Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate through an HTML table column and input the values into a Javascript array using JQuery

Let's say I have a table column with 10 rows, each with <td id="num"> and a text value.

How can I use JQuery to loop through each row in the column and input the spins into a Javascript array?

I thought the following code would do it, but it is only getting the first element:

var numArray = [];
$("#num").each(function(n){
   numArray[n] = $(this).text();
});

Any ideas?

Thanks!

like image 463
PleaseHelpMe Avatar asked Jul 30 '26 22:07

PleaseHelpMe


1 Answers

You can't have multiple elements with the same id. This isn't allowed because the id is used to identify individual elements in the DOM. I'd suggest giving them all the same class, which is allowed.

<td class="num">

Then this should work:

var numArray = [];
$(".num").each(function(n){
   numArray[n] = $(this).text();
});
like image 160
Mark Costello Avatar answered Aug 01 '26 12:08

Mark Costello



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!