Use addEventListener() for most button clicks, and reserve onclick for tiny demos, quick tests, or very small pages. It keeps JavaScript separate from HTML, supports multiple handlers, and gives better control over event behavior. MDN lists addEventListener() as widely available across major browsers since July 2015 [source: MDN].
TLDR: For a real interface, button.addEventListener('click', handler) is usually the safer choice than button.onclick = handler or inline onclick="...". For example, a shopping cart with 12 buttons may need tracking, validation, and UI updates on the same click; addEventListener() can attach all three without one replacing another. In a small internal test page with one button and one action, onclick may still be fine. A team maintaining 40 or more interactive buttons will usually save time by using one consistent event listener pattern.
What onclick Means
onclick is an event handler property. It can appear directly in HTML, or it can be assigned in JavaScript. Both forms react when a user clicks a button.
<button onclick="saveForm()">Save</button>
const button = document.querySelector('#save');
button.onclick = function () {
saveForm();
};
This style is easy to read at first. A beginner can see the connection between the button and the action right away. That simplicity is the main reason onclick still appears in tutorials, prototypes, admin panels, and small widgets.
The problem shows up when the page grows. A button may need one function for saving, another for analytics, and another for changing the label to Saving…. With onclick, assigning a new handler can replace the old one. That is a quiet bug, which is the worst kind. It looks harmless until a feature stops firing.
What addEventListener() Does Better
addEventListener() attaches a function to an event without taking ownership of the whole event slot. Several listeners can react to the same button click.
const button = document.querySelector('#save');
button.addEventListener('click', saveForm);
button.addEventListener('click', trackSaveClick);
button.addEventListener('click', showSavingState);
That is the practical win. The button can support separate behavior from separate parts of the codebase. One script can manage the user interface. Another can send analytics. Another can run validation. They do not need to fight over a single onclick property.
The annoying part is that addEventListener() takes a few more characters to type. On a one-button demo, that feels like ceremony. On a production page, those extra seconds pay for themselves when the second feature arrives.
Inline onclick vs JavaScript onclick
There are two common onclick patterns, and they are not quite the same.
- Inline HTML:
<button onclick="send()"> - JavaScript property:
button.onclick = send;
Inline handlers mix markup and behavior. That can make HTML harder to scan. It can also create trouble when a project uses stricter security rules, such as a Content Security Policy that blocks inline scripts.
The JavaScript property version is cleaner because behavior stays in JavaScript. Still, it has the same main flaw: the property holds one handler. If another script assigns button.onclick, the earlier one is gone.
button.onclick = validateForm;
button.onclick = submitForm; // validateForm has now been replaced
That tiny overwrite can cost real debugging time. It drives developers crazy because there is no loud error. The browser simply does what the code asked.
When onclick Is Acceptable
onclick is not evil. It is just limited. It can be acceptable in simple cases.
- A one-file demo used in a lesson.
- A quick proof of concept.
- A small personal page with one or two actions.
- A throwaway admin tool used by one person.
In these cases, speed may matter more than structure. If a button only opens a panel or changes a color, inline onclick may not hurt anything. Still, many teams use addEventListener() even for small tasks because it builds the right habit.
When addEventListener() Is the Better Choice
addEventListener() is the better fit when a page has more than a few interactions. It is also better when several developers work on the same code.
- Multiple actions: one click can call several functions.
- Cleaner files: HTML stays focused on structure.
- Reusable code: handlers can be attached to many buttons.
- Safer updates: new listeners do not replace old listeners.
- More options: listeners can use settings such as
onceandcapture.
For example, a sign-up button might need to disable itself, check required fields, show a spinner, and send a tracking event. With addEventListener(), each piece can stay separate.
const signup = document.querySelector('#signup');
signup.addEventListener('click', disableButton);
signup.addEventListener('click', validateFields);
signup.addEventListener('click', sendAnalytics);
Event Options That onclick Does Not Offer
addEventListener() can accept an options object. This gives extra control.
button.addEventListener('click', submitForm, {
once: true
});
With once: true, the handler runs one time and then removes itself. That is useful for payment buttons, modal confirmations, and setup steps that must not run twice.
Another option is capture, which changes when the event is handled during event flow. Most button clicks do not need it, but larger interfaces sometimes do. There is also passive, often used with touch and scroll events, though it is less common for standard button clicks.
Removing Click Behavior
addEventListener() also pairs with removeEventListener(). This matters in apps that create and destroy views, menus, dialogs, or widgets.
function showMessage() {
console.log('Clicked');
}
button.addEventListener('click', showMessage);
button.removeEventListener('click', showMessage);
The same function reference must be used. Anonymous functions are harder to remove because there is no saved reference.
button.addEventListener('click', function () {
console.log('Clicked');
});
That code works, but removing it later is awkward. For long-lived pages, named functions usually age better.
Event Delegation for Many Buttons
When a page has many buttons, adding a listener to each one can feel clunky. Event delegation solves that. A parent element listens for clicks and checks which child button was clicked.
const toolbar = document.querySelector('#toolbar');
toolbar.addEventListener('click', function (event) {
if (event.target.matches('button[data-action]')) {
const action = event.target.dataset.action;
runAction(action);
}
});
This pattern is useful for tables, menus, product cards, and generated lists. If 100 rows each have an edit button, the parent can handle all clicks with one listener. That can reduce setup code and make newly added buttons work without extra wiring.
Best Practice Recommendation
For modern button interactions, a developer should usually follow this pattern:
- Use semantic
<button>elements for clickable actions. - Select buttons in JavaScript with clear IDs, classes, or data attributes.
- Attach behavior using
addEventListener('click', handler). - Use named functions when behavior may need to be removed.
- Avoid inline
onclickin shared or production code.
A solid default looks like this:
<button id="deleteItem" type="button">Delete</button>
<script>
const deleteButton = document.querySelector('#deleteItem');
function confirmDelete() {
console.log('Delete confirmed');
}
deleteButton.addEventListener('click', confirmDelete);
</script>
This keeps the HTML readable and the behavior easy to test. It also leaves room for more features later. That matters because buttons rarely stay simple for long.
FAQ
-
Is
onclickbad practice?
Not always. It is fine for quick demos or tiny pages. For shared, growing, or production code,addEventListener()is usually cleaner and safer. -
Can a button have multiple
onclickhandlers?
Not through the same property. A laterbutton.onclick = ...assignment replaces the earlier one.addEventListener()allows multiple handlers. -
Which is better for beginners?
onclickis easier to understand at first. Still, beginners benefit from learningaddEventListener()early because it matches real project patterns. -
Does
addEventListener()work with all buttons?
Yes. It works with regular<button>elements, submit buttons, menu buttons, and buttons created with JavaScript. -
Should inline
onclickbe used in HTML?
Usually no. It mixes structure and behavior. External or separate JavaScript is easier to maintain, test, and update.



