Skip to content
Advertisement

detect outside table row click

How can I detect if a mouse click was not done on a table row?

I created this table, which can be modified on the fly and highlights the selected row, if selected. Now I try to establish a function, which removes the row highlighting as soon as a mouse click was not done in one of those table rows. I do not want to add another eventListener for the remaining body or other html elements located in the background.

I thought about the following:

if (row highlighted) { next outside click removes row highlight }

JavaScript

Advertisement

Answer

Much simpler solution for this will be

  • Add an event listner to the click event of body.
  • Check whether the ckick event is either on the button or on the table with Node.contains in Javascript Reference
  • If the table row and button is not clicked, reset the color.

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