I have a table that handles mouse clicks using onclick for the entire row. The problem is that these rows also contain checkboxes that should not be activating the onclick event. Clicking on a row should cause an event but selecting a checkbox should not.
I found a lot of answers to this but I've been unable to make it work. I'm using simple html/css/js without anything like jQuery.
The answer seems to be due to event propagation and lots of answers say to use stopPropagation(). But Chrome says this function undefined for the event.
Here is my test example.
Clicking on the table results in a "table!" alert. I don't want clicks on the checkbox within the table to cause this alert.
function clickTable(e) {
alert('table!');
}
function clickCheckbox(e) {
// Error: e.stopPropagation is not a function
e.stopPropagation();
}
table {
border: 1px solid black;
width: 120px;
height: 120px;
}
input {
width: 32px;
height: 32px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Checkbox Click</title>
</head>
<body>
<table>
<tr onclick="clickTable(this);">
<th><label>
<input type="checkbox" name="test" onclick="clickCheckbox(this);">
</label></th>
</tr>
</table>
<script>
</script>
</body>
</html>
I can however use window.event.stopPropagation(); which works as expected. But my IDE PhpStorm reports that using window.event in this way is deprecated.
What is the correct use of stopPropagation() for this event?
