Saustin Saustin - 1 year ago 75
Ajax Question

Assigning unique value to buttons on a table [JS/jQuery]

So I have a table with multiple buttons. I want to assign a numerical ID to each of these. When the button is pressed, it will do a ajax call.

Is it possible to do something like:

<tr><td><button id="btn-0"></tr></td> <!-- generated via DOM !-->
<tr><td><button id="btn-1"></tr></td>
<tr><td><button id="btn-n"></tr></td>

for(i = 0; i < n; i++) {
$("#btn-" + i).click(doAjax(i));

I feel like there is a much better way to do this that I have not thought of. What is a good way to represent an array of buttons that is both HTML and JS friendly?

Answer Source

I'd advise you to but them in a class, and then give them their ids like this:

<button class="button" id="1"></button>

You can then use this javascript:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download