<p id=”text”>

<button onclick=”showName()”>My name is?</button>

<script>

function showName() {

document.getElementById(‘text’).innerHTML=‘Paul’

}

</script>

This extracts the code out into its own function, which makes the code far more maintainable and easier to read. This is the preferred method of assigning actions to events.

As we established earlier, there are many different events that JavaScript can react to. The full list of events we can watch out for are as follows:

We can make use of any of these events by simply adding the event text to the responsible HTML element like we did before. Let’s see a few examples of this in action.