Browser Events

Listening to Browser Events

At a certain time, there can be many events going on in the browser, like mouse clicks, mouse movements, key presses, among others. We need the browser to wait for a particular event to occur, and after that perform a set of instructions.

Look at the example given below:

In the above example, we are listening to a few types of events on the p DOM object using the addEventListener method. This listener method will monitor the DOM object for a particular event.

In addEventListener, we specify the name of the event and the code that we want to execute once that particular event occurs. For example, as soon as someone copies the code from the p element, we log to the console that data was copied.