Most production code should use addEventListener() instead of inline onclick attributes, because it keeps behavior out of markup, supports multiple handlers, and scales better as pages grow. MDN states that addEventListener() can attach more than one handler to the same target without overwriting earlier handlers [source: MDN]. Inline JavaScript still has a place in tiny demos, throwaway prototypes, and quick admin tools. For maintainable web apps, though, separate scripts win almost every time.
TLDR: Inline onclick is quick, but addEventListener() is cleaner and safer for real projects. If a product page has 30 “Add to cart” buttons, one delegated listener can handle them all instead of repeating onclick="addToCart()" 30 times. In one common team scenario, moving shared button logic from inline handlers to a single listener file can cut repeated event code by 60% or more. That means fewer copy-paste bugs and less time spent wondering why one button behaves differently from the other 29.
What inline onclick does
An inline handler places JavaScript directly inside an HTML element. It often looks simple:
<button onclick="saveProfile()">Save</button>
This style feels fast. A developer can see the element and the action in one place. For a tutorial, a single static page, or a quick internal test, that can be fine.
The catch is that the HTML starts carrying too much responsibility. It describes structure, but it also controls behavior. As a page grows, the markup turns into a messy mix of buttons, attributes, function names, and small bits of logic. It drives people a little mad when a simple rename breaks five hidden inline calls across three templates.
Inline handlers also make reuse harder. If several buttons need similar behavior, each one may get its own attribute. That invites drift. One button gets updated. Another does not. Bugs appear in places that look almost identical.
What addEventListener() does
addEventListener() attaches an event handler from JavaScript, not from the HTML attribute. The markup stays cleaner:
<button id="saveButton">Save</button>
<script>
const button = document.querySelector("#saveButton");
button.addEventListener("click", function () {
saveProfile();
});
</script>
This approach separates content from behavior. HTML defines the button. JavaScript defines what happens after the click. CSS can still handle style. Each layer has a clearer job.
It also allows several listeners on the same element:
button.addEventListener("click", saveProfile);
button.addEventListener("click", showSavingMessage);
button.addEventListener("click", trackSaveEvent);
That is useful in real work. A product team may need one handler for the user action, one for analytics, and one for UI feedback. With inline onclick, stacking those concerns often becomes awkward.
Main differences between the two methods
- Location: Inline
onclicklives in HTML.addEventListener()lives in JavaScript. - Multiple handlers: Inline event attributes are limited and easier to overwrite.
addEventListener()supports multiple handlers on the same target. - Maintainability: Inline code spreads behavior across templates. Listener-based code can sit in organized modules.
- Testing: Separate JavaScript is usually easier to test, mock, and review.
- Security: Inline JavaScript can conflict with stricter Content Security Policy rules.
- Reuse: Listener functions can be shared across elements and components.
Why inline JavaScript becomes painful
Small shortcuts get expensive. A site may begin with three buttons. Six months later, it has account menus, modals, filters, cart controls, and tracking pixels. Inline handlers then scatter through server templates, CMS blocks, and partial views.
Honestly, it feels like a trap when fixing a click bug takes 20 seconds in JavaScript but 15 minutes of searching through template files. The code is not always broken. It is just hiding in the wrong place.
Inline handlers can also expose functions globally. For this to work:
<button onclick="openMenu()">Menu</button>
openMenu() often needs to exist on the global window scope. Modern JavaScript modules avoid that pattern. They keep functions private by default. That reduces name clashes and makes code easier to reason about.
Why addEventListener() scales better
A listener can be attached to one element, many elements, or a parent container. This matters for lists, tables, search results, and product grids.
For example, a page with 100 delete buttons does not need 100 inline handlers. It can use event delegation:
const list = document.querySelector("#taskList");
list.addEventListener("click", function (event) {
if (event.target.matches(".deleteButton")) {
deleteTask(event.target.dataset.id);
}
});
The parent listens once. When a click bubbles up, the code checks whether the clicked element matches the right selector. This pattern works well when items are added after the first page load.
addEventListener() also accepts options such as once, capture, and passive. These give finer control over how events behave. A one-time confirmation listener can remove itself automatically:
button.addEventListener("click", confirmAction, { once: true });
That is cleaner than manually removing the handler in many cases.
When inline onclick is still acceptable
Inline JavaScript is not evil. It is just easy to overuse. It can be acceptable when the page is tiny, temporary, and not shared across a larger codebase.
Good uses may include:
- Learning examples where the goal is to show a click event in one line.
- Quick prototypes that will be deleted or rebuilt soon.
- Small internal pages with one or two simple controls.
- Debug tests where speed matters more than structure.
Even then, the team should avoid putting complex logic inside the attribute. This is poor practice:
<button onclick="if(user.loggedIn){save();track();closeModal();}">Save</button>
That kind of inline logic is hard to read, hard to debug, and easy to break.
Best practice for modern web event handling
The better default is simple:
- Use semantic HTML for structure.
- Use CSS for presentation.
- Use JavaScript files or modules for behavior.
- Use
addEventListener()for clicks, input changes, keyboard events, and form actions. - Use event delegation for repeated elements.
- Keep handler functions small and named when the logic is reused.
A clean pattern may look like this:
function handleSaveClick(event) {
event.preventDefault();
saveProfile();
}
document
.querySelector("#saveButton")
.addEventListener("click", handleSaveClick);
This is clear. The function has a name. The event is explicit. The HTML stays readable. Code review also gets easier because behavior changes appear in JavaScript files, not buried inside markup strings.
FAQ
Is onclick bad in JavaScript?
No. onclick is not bad by itself. It is just limited. Inline onclick works for small examples, but addEventListener() is usually better for maintainable projects.
Can onclick and addEventListener() be used together?
Yes, they can run on the same element. Still, mixing them can confuse teams. A project should usually pick one pattern, and addEventListener() is the cleaner choice.
Which one is better for performance?
For a few elements, the difference is usually tiny. For many repeated elements, event delegation with addEventListener() can reduce repeated code and memory use.
Does addEventListener() work for events besides clicks?
Yes. It works with many event types, including input, submit, keydown, mouseover, change, and custom events.
What is the best default choice?
For real web apps, addEventListener() should be the default. Inline onclick should be saved for demos, quick tests, and very small pages.