A click on a list element selects only that element (adds the class, If you have suggestions what to improve - please. How do I test for an empty JavaScript object? To invoke the handler, we use the name of the variable and not that of the function when defining the onclick event handler. The code then prints the reference to the console, which is simply the HTML for defining the button: Now that we have a reference to the element, we can use the reference to assign an event handler. code2 just triggers it. You can add many event handlers to one element. In this particular case the most reasonable way is to prevent the browser action on mousedown. WebJavaScript Event Handlers Event handlers can be used to handle and verify user input, user actions, and browser actions: Things that should be done every time a page loads In the following example, we have a single