JavaScript Prevent checkbox and clickable table row conflict

I have a clickable table row and checkbox in that row. When user click on that row, user will be redirected to other page. That was expected behavior. Now the problem is when user click on checkbox, user also will be redirected to other page. This is not the expected behavior. Clicking on checkbox should not trigger redirect() method

  handleChange(e) {
    this.setState({
      checkbox: e.target.checked,
    });
  }

  redirect() {
    Router.push('/registration/register/RegisterEditor', '/verification/e7fe5b68-94e8-435f-8303-5308fd1f7e69');
  }

              <tbody>
                {inventory.list().map((data, index) => (
                  <tr key={'asset'.concat(index)} onClick={() => { this.redirect(); }} tabIndex={index + 1} role="button">
                    <td className="text-center">{index + 1}</td>
                    <td>{data.item}</td>
                    <td width="3%">
                      <Input className="mx-auto" type="checkbox" onChange={this.handleChange} />
                    </td>
                  </tr>
                ))}
              </tbody>

Output:

enter image description here

How can I solve this problem? Thanks in advance.

Answer:1

Have a look at this snippet: https://codesandbox.io/s/qx6Z1Yrlk

You have two options:

Adding an if-statement in your redirect function checking what element has been clicked on and only redirect if it's the row (make sure you pass in the event).

Or, listening for a click event on the checkbox as well, passing in the event, and stop the event from bubbling to the row element. stopPropagation won't work in the change event listener as the click event is fired before the change event.

Answer:2

I am new to javascript, The below question should return 1 according to my knowledge about javascript, but it's returning "undefined". could anyone tell me why it's not returning 1? var foo = { ...

I am new to javascript, The below question should return 1 according to my knowledge about javascript, but it's returning "undefined". could anyone tell me why it's not returning 1? var foo = { ...

I got an interface,,already logged in with a new username now i want so select this user to my selected option value and insert it into Mysql DB.. <option value="" required="required"> Please ...

I got an interface,,already logged in with a new username now i want so select this user to my selected option value and insert it into Mysql DB.. <option value="" required="required"> Please ...

  1. select current logged in user oracle
  2. sql select current logged in user

I am trying to get translated text from content management system with service URL. When I use a JSON file it works well, but how can I use a service URL to get translated data? This is the code ...

I am trying to get translated text from content management system with service URL. When I use a JSON file it works well, but how can I use a service URL to get translated data? This is the code ...

I have an angular application, by using $stateProvider i have configured few angular states. but when i target to any state, the url appear '#' character like: http://localhost:63808/#/login, instead ...

I have an angular application, by using $stateProvider i have configured few angular states. but when i target to any state, the url appear '#' character like: http://localhost:63808/#/login, instead ...

  1. ignore synonym
  2. ignore definition
  3. ignore me
  4. ignore me meme
  5. ignore in spanish
  6. ignore antonym
  7. ignore memes
  8. ignore him
  9. ignore meaning
  10. ignore gif
  11. ignore unscramble
  12. ignore warnings python
  13. ignore her
  14. ignore me venture bros
  15. ignore case in java
  16. ignore all errors in excel
  17. ignore case python
  18. ignore quotes
  19. ignore syn
  20. ignore na in r