charlesbarron1999
charlesbarron1999 7d ago • 20 views

JavaScript 'onclick' event examples for beginners

Hey there! 👋 Ever wondered how to make your website buttons actually *do* something when someone clicks them? The `onclick` event in JavaScript is your answer! It's like magic that brings your web pages to life. Let's dive into some super simple examples and then test your knowledge with a quick quiz. Ready to become a JavaScript wizard? ✨
💻 Computer Science & Technology
🪄

🚀 Can't Find Your Exact Topic?

Let our AI Worksheet Generator create custom study notes, online quizzes, and printable PDFs in seconds. 100% Free!

✨ Generate Custom Content

1 Answers

✅ Best Answer
User Avatar
karen_clark Dec 29, 2025

📚 Quick Study Guide

  • 🖱️ The `onclick` event is triggered when an HTML element is clicked.
  • ✍️ You can add the `onclick` attribute directly to HTML elements like buttons, divs, or spans.
  • ✨ The value of the `onclick` attribute is a JavaScript code snippet that will be executed when the element is clicked.
  • 📢 Common uses include triggering functions, displaying alerts, changing content, and submitting forms.
  • 💡 Best practice: Use JavaScript files to keep HTML clean and separate behavior from structure. Add event listeners using `addEventListener()` method.
  • 🔗 Example: ``

🧪 Practice Quiz

1. Which HTML attribute is used to specify code to be executed when an element is clicked?
  1. `onhover`
  2. `onclick`
  3. `onpush`
  4. `onactivate`
2. What will happen when the following button is clicked? ``
  1. The page will reload.
  2. The `myFunction()` will be executed.
  3. Nothing will happen.
  4. An error will occur.
3. Which of the following is the correct way to add an event listener to a button using JavaScript?
  1. `button.addEventListener(click, myFunction);`
  2. `button.addEventListener("click", myFunction);`
  3. `button.on('click', myFunction);`
  4. `button.onClick = myFunction();`
4. What is the purpose of the `onclick` event?
  1. To style an HTML element.
  2. To execute JavaScript code when an element is clicked.
  3. To change the text color.
  4. To hide an element.
5. Which of the following JavaScript code will display an alert box when a button is clicked?
  1. ``
  2. ``
  3. ``
  4. ``
6. What does the following code do? `
Click Me
`
  1. It changes the background color of the div.
  2. It displays an alert box.
  3. It changes the text inside the div to 'Clicked!'.
  4. It hides the div.
7. Why is it generally recommended to use `addEventListener` instead of inline `onclick` attributes?
  1. `addEventListener` is shorter to write.
  2. `addEventListener` allows for better separation of concerns (HTML structure vs. JavaScript behavior).
  3. Inline `onclick` attributes are faster.
  4. There is no difference between them.
Click to see Answers
  1. B
  2. B
  3. B
  4. B
  5. B
  6. C
  7. B

Join the discussion

Please log in to post your answer.

Log In

Earn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! 🚀