Events
Learn how JavaScript responds to actions such as button clicks.
What you should understand
Read the lesson carefully, study the example, then use the editor below to experiment with the code yourself.
<!DOCTYPE html>
<html>
<body>
<button onclick="sayHello()">Click Me</button>
<p id="output"></p>
<script>
function sayHello() {
document.getElementById("output").textContent =
"You clicked the button!";
}
</script>
</body>
</html>
Output
See what your code produces.
Test your understanding
Choose the best answer.
Which method can select an element by its ID?