0

I want to have a function that responds to a click on each instance of an input field. But the function only responds to a click on the first instance of the input.

The HTML is:

<table class="table">
  <tr>
     <td><input id="show-output" type="button" value=261 /></td>
     <td>mhour test202</td>
  </tr>
  <tr>
     <td><input id="show-output" type="button" value=260 /></td>
     <td>mhour test145</td>
  </tr>
</table>

The function is

$('#show-output').click(function(e) {
  alert("hi");
});

A fiddle for this

spodell
  • 157
  • 12

1 Answers1

1

Change your ids to classes, as an id should always appear just once on a page.

$('.show-output').click(function(e) {
  alert("hi");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<table class="table">
  <tr>
     <td><input class="show-output" type="button" value=261 /></td>
     <td>mhour test202</td>
  </tr>
  <tr>
     <td><input class="show-output" type="button" value=260 /></td>
     <td>mhour test145</td>
  </tr>
</table>
JonSnow
  • 573
  • 14
  • 48