Darin
Darin

Reputation: 1

What is the best way to create a HTML table with random numbers

I was able to use jQuery to create a random number and put it in a <td>. But can I make each <td> have its own random number?

Upvotes: 0

Views: 1482

Answers (1)

David Thomas
David Thomas

Reputation: 253328

I'd suggest:

$('td').text(function(){
    return Math.random(); // or however you want to generate your random number
});

JS Fiddle demo.

The above iterates through each td element and sets its text independently of other td elements, unlike:

$('td').text(Math.random());

JS Fiddle demo.

Which sets the same random number to all the td elements.

References:

Upvotes: 9

Related Questions