1

I have a Clickable table row on clicking of which I am performing some operations and inside that row I have a button with some onClick event happening on that.

 <tr onClick={this.update} style={{cursor:'pointer'}}>
                <td className="col-sm-1 col-md-1 text-center"><strong>Some content</strong></td>
                <td className="col-sm-1 col-md-1">
                    <button type="button" className="btn btn-danger pull-right"
                    onClick={this.removeData}>
                    <span className="glyphicon glyphicon-remove"></span>
                    </button>
                </td>
 </tr>

But whenever I try to click on button inside tr onClick of tr gets fired. I want to perform action based on the click of button also.

WitVault
  • 23,445
  • 19
  • 103
  • 133

3 Answers3

3

Please, take a look at this JS Fiddle example http://jsfiddle.net/ric/02ofa45c/

'$(document).ready(function () {
  $('.outer').on('click', function () {
    console.log('outer');
  });

  $('.inner').on('click', function (e) {
    e.stopPropagation();
    console.log('inner');
  });
});
Ric.H
  • 489
  • 4
  • 12
2

Use event.stopPropagation() as the first statement inside your removeData handler for the button. And make sure the event is available as a function argument.

Robusto
  • 31,447
  • 8
  • 56
  • 77
  • Got it. In React event handlers have instances of `SyntheticEvent`, a cross-browser wrapper on which I will call `stopPropagation()` – WitVault Dec 08 '15 at 19:40
2

Add event.stopPropagation() inside the onclick event of the child. This will prevent the child event function propagating to the parent event.

In the JS or JQuery function :

function parentEvent(){
       //perform some action
}

function childEvent(){
       event.stopPropagation()
       //perform some other action
}

This will hopefully solve your problem

It Assistors
  • 998
  • 2
  • 14
  • 29