Single handler for multiple elements in JavaScript

A single function can be bound to multiple elements at once. For example, let's say we have the following function:

function func() { console.log('!!!'); }

There are also two buttons:

<input id="button1" type="submit" value="button1"> <input id="button2" type="submit" value="button2">

Get references to these buttons into variables:

let button1 = document.querySelector('#button1'); let button2 = document.querySelector('#button2');

And now let's bind our function func to both the first and the second button:

button1.addEventListener('click', func); button2.addEventListener('click', func);

Given 5 paragraphs:

<p id="elem1">text</p> <p id="elem2">text</p> <p id="elem3">text</p> <p id="elem4">text</p> <p id="elem5">text</p>

Given the following function:

function func() { console.log('message'); }

Bind this function to all 5 paragraphs.

enru