×

Save Your Code

If you click the save button, your code will be saved, and you get an URL you can share with others.

By clicking the "Save" button you agree to our terms and conditions.

Report Error

×

Save to Google Drive

If you have a Google account, you can save this code to your Google Drive.

Google will ask you to confirm Google Drive access.

×

Open from Google Drive

If you have saved a file to Google Drive, you can open it here:

Result Size: 625 x 571
x
 
<!DOCTYPE html>
<html>
<body>
<style>
div {
  padding:50px;
  background-color:rgba(255, 0, 0, 0.2);
  text-align:center;
  cursor:pointer;
}
</style>
<h1>The cancelBubble Property</h1>
<p>Click DIV 1:</p>
<div onclick="func2()">DIV 2
  <div onclick="func1(event)">DIV 1</div>
</div>
Cancel bubble:
<input type="checkbox" id="check">
<p></p>
<p>Because DIV 1 is inside Div 2, both DIVs get clicked when you click on DIV 1.</p>
<p>Check the cancel bubble checkbox, and try again.</p>
<p>The cancelBubble = true prevents the event flow from bubbling up to parent elements.</p>
<script>
function func1(event) {
  alert("DIV 1");
  if (document.getElementById("check").checked) {
    event.cancelBubble = true;
  }
}
function func2() {
  alert("DIV 2");
}
</script>
</body>
<!-- Mirrored from www.w3schools.com/jsref/tryit.asp?filename=tryjsref_event_cancelbubble by HTTrack Website Copier/3.x [XR&CO'2014], Wed, 05 Jun 2019 14:45:41 GMT -->
</html>
×

Report a Problem: