Type something to search...

JavaScript Events

JavaScript Events

Events are actions or occurrences that happen in the browser, such as clicks, key presses, or mouse movements.

Adding an Event Listener

You can use the addEventListener method to attach an event to an element:

document.getElementById("myButton").addEventListener("click", function() {
    console.log("Button clicked!");
});

Inline Event Handlers

You can also add events directly in HTML:

<button onclick="alert('Button clicked!')">Click Me</button>

Example

<!DOCTYPE html>
<html>
<body>
    <button id="myButton">Click Me</button>
    <script>
        document.getElementById("myButton").addEventListener("click", function() {
            alert("Button was clicked!");
        });
    </script>
</body>
</html>

Next: DOM Manipulation →