Home

onClick

OnClick refers to a handler invoked when a user activates an element with a pointing device or keyboard. It is used in HTML, JavaScript, and UI frameworks to run code in response to a click. In markup it appears as onclick, in the DOM as a property named onclick, and in event-driven code via addEventListener('click', …). Frameworks commonly expose onClick as a camelCase prop.

HTML usage: <button onclick="doSomething()">Click me</button> shows an inline handler that executes in the global scope. Inline

JavaScript handling: element.addEventListener('click', handler) registers a listener without replacing others, and supports removal with removeEventListener. The

Accessibility: Use native button, input, or anchor elements for clickable controls. If non-semantic elements are used,

Best practices: prefer addEventListener over inline handlers, detach listeners when no longer needed, and test across

handlers
couple
markup
and
behavior
and
can
complicate
maintenance
or
security.
Prefer
attaching
handlers
with
JavaScript,
which
keeps
structure
separate
from
behavior.
onclick
property
can
hold
only
a
single
handler.
Click
events
bubble
up
through
the
DOM,
so
a
listener
on
a
parent
can
capture
clicks
from
descendants
unless
stopPropagation
or
event.stopImmediatePropagation
is
used.
apply
role='button',
ensure
keyboard
activation
(Enter/Space),
and
provide
visible
focus
indicators.
Clear
labels
and
accessible
names
help
users
relying
on
assistive
technologies.
Consider
replacing
non-semantic
clickable
elements
with
proper
controls
when
possible.
devices.
In
React
and
similar
frameworks,
onClick
is
typically
handled
through
framework-specific
event
systems.