Source Code:
(back to article)
Submit
Result:
Report an issue
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Event Capture and Bubbling Example</title> <style> body { font-family: Arial, sans-serif; margin: 0; padding: 20px; } #outerContainer { border: 2px solid #ccc; padding: 20px; margin-bottom: 20px; background-color: #f9f9f9; border-radius: 10px; } #innerElement { background-color: #ffa8a8; padding: 10px; border-radius: 5px; cursor: pointer; } </style> </head> <body> <div id="outerContainer" onclick="alert('Event Bubbled from Outer Container');"> <p style="margin: 0;">Click anywhere in this outer container:</p> <p id="innerElement">Click me!</p> </div> <script> // Event listener attached to the outer container during the capturing phase document.getElementById('outerContainer').addEventListener('click', function() { alert('Event Captured by Outer Container'); }, true); // Event listener attached to the inner element during the bubbling phase document.getElementById('innerElement').addEventListener('click', function() { alert('Event Bubbled from Inner Element'); }, false); </script> </body> </html>