Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent table row onclick event to fire when clicking button inside the row

I have a table row with ONCLICK event (that toggles additional data below). Inside one of the row cells I have a button (when clicked is performing an AJAX action). When I click on the button - the Row's onclick event also fires and what happens is that the additional data appears before the AJAX call completes (which is a bad behavior for me). Any ideas how can I solve this elegantly? (without identifying and coding it into the row's onclick code) Thanks

like image 894
Guy Avatar asked Jun 27 '11 10:06

Guy


People also ask

How do I disable the <TR> onClick event?

Add an event.stopPropagation (); to your buttons click handler. For more information, have a look here. If you dislike the javascript solution, in many cases a CSS solution is possible as well: That will supress the <tr> onclick event. However, in some cases I still had issues where it didn't seem to work.

How to stop the event from propagating with inline onclick attribute?

Use HTML DOM stopPropagation () method to stop the event from propagating with inline onclick attribute which is described below:

How to stop event propagation in HTML?

Example 1: This example stops the event propagation by adding stopPropagation method on onclick the <span> element. Example 2: This example stops the event propagation by adding stopPropagation () method on onclick to the <span> element. This example handles the Internet Explorer case by setting window.event.cancelBubble to true.


1 Answers

Add an event.stopPropagation(); to your buttons click handler. For more information, have a look here.

like image 194
Niklas Avatar answered Oct 08 '22 11:10

Niklas