0

If I have this:

<td name="id1">First</td> <button>btn1</button>
<td name="id2">Second</td> <button>btn2</button>
<td name="id3">Third</td> <button>btn3</button>
<td name="id4">Fourth</td> <button>btn4</button>

I want if I click (btn1) the form send to my controller and then I can have (id2) the ids generated by database so I don't know what is the number, how can I get the number by clicking the same side button?

Yazan Mehrez
  • 1,174
  • 2
  • 10
  • 23

1 Answers1

0

Instead of using name, use ID

<td id="id1">First</td> <button>btn1</button>
<td id="id2">Second</td> <button>btn2</button>
<td id="id3">Third</td> <button>btn3</button>
<td id="id4">Fourth</td> <button>btn4</button>

Then use JavaScript like so:

document.getElementById('id1').innerHTML; // this is the value

or

document.getElementById('id1').id; // this is the value

or

document.getElementById('id1').value; // this is the value

To make the button work:

<td id="id1">First</td> <button id='btn1' onclick="handleButton(this);">btn1</button>
<td id="id2">Second</td> <button  id='btn2'onclick="handleButton(this);">btn2</button>
<td id="id3">Third</td> <button  id='btn3'onclick="handleButton(this);">btn3</button>
<td id="id4">Fourth</td> <button  id='btn4'onclick="handleButton(this);">btn4</button>

The JavaScript function:

function handleButton(obj) {
    alert('You clicked: ' + obj.id);
}