Free Ebook cover Complete HTML Course

Complete HTML Course

4.38

(8)

37 pages

Events in HTML

Capítulo 14

Estimated reading time: 3 minutes

Audio Icon

Listen in audio

0:00 / 0:00

Events in HTML are actions or occurrences that happen on a page element, such as a mouse click, key press, or page load. These events can be handled with JavaScript to perform specific actions.

Types of events

There are several types of events in HTML, including:

  • Mouse events: like click, double click, mouse movement, etc.
  • Keyboard events: like pressing a key, releasing a key, etc.
  • Form events: how to submit a form, change a form field, etc.
  • Loading events: like loading a page, loading an image, etc.

Handling events with JavaScript

To handle events in HTML, you must use JavaScript. JavaScript allows you to define functions that will be executed when a specific event occurs on a page element.

For example, to run a function when a button is clicked, you could use the following code:

  
    <button onclick="myFunction()">Click here</button>

    <script>
      function myFunction() {
        alert("The button was clicked!");
      }
    </script>
  

In this example, the function "myFunction()" will be executed when the button is clicked. The function displays an alert with the message "The button was clicked!".

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

Adding events with JavaScript

It is also possible to add events with JavaScript. This allows you to define functions to be executed when a specific event occurs on a page element, without having to use the "onclick" attribute.

To add an event with JavaScript, you can use the "addEventListener()" method. For example, to run a function when a button is clicked, you could use the following code:

  
    <button id="myButton">Click here</button>

    <script>
      document.getElementById("myButton").addEventListener("click", myFunction);

      function myFunction() {
        alert("The button was clicked!");
      }
    </script>
  

In this example, the "addEventListener()" method is used to add a click event to the button with the ID "myButton". The "myFunction()" function is defined as the function to be executed when the click event occurs.

Conclusion

Events in HTML are an important part of creating interactive and dynamic web pages. With JavaScript, it is possible to handle events to perform specific actions when a user interacts with a web page.

Now answer the exercise about the content:

_Which method is used to add an event to a page element with JavaScript?

You are right! Congratulations, now go to the next page

You missed! Try again.

The method addEventListener() is used to add an event to a page element with JavaScript. This method allows you to specify the type of event and the function to execute when the event occurs, allowing for more flexibility and separation of HTML and JavaScript code.

Next chapter

Form validation with JavaScript

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.