Skip to content
Advertisement

Only first row button is working in a table

Problem: In the complete column, there is yes button after clicking on it, it show consulted but problem is only one row is working. I don’t understand how i defined each row uniquely. Please have a look at this code and let me know what mistake I’m doing here.

JavaScript

Advertisement

Answer

The issue is because you’re using id attributes in the repeated content which creates duplicates. id have to be unique.

To fix the issue change that id to a class, then you can create an event handler to listen for any click on any of the button elements. From there you can use the Event object passed to the event handler to update only the button which was clicked.

Note in the example below the use of an unobtrusive event handler instead of an inline onclick attribute. The latter is not good practice and should be avoided. Also note that the event is delegated to the parent table. This allows us to use a single event handler no matter how many rows/buttons the table contains, or if they are dynamically created after the DOM loads.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement