Attach or detach an event handler
Written byPhuoc Nguyen
Created
17 Feb, 2020
Last updated
18 Jun, 2020
Category
Level 1 — Basic
#Use the on attribute (not recommended)
You can set the event handler via the
`on{eventName}`
attribute, where `eventName`
represents the name of event.
For example:js
ele.onclick = function() {
...
};
// Remove the event handler
delete ele.onclick;
This approach isn't recommended because we can only attach one handler for each event. Setting the
`onclick`
attribute, for example, will override any existing handler for the `click`
event.#Use the addEventListener method
js
const handler = function() {
...
};
// Attach handler to the `click` event
ele.addEventListener('click', handler);
// Detach the handler from the `click` event
ele.removeEventListener('click', handler);
Note that the event name is passed as the first parameter in both the
`addEventListener`
and `removeEventListener`
methods.
It differs from the first approach which requires to prefix the event name with `on`
.If you want the handler to be invoke once, then look at the Create one time event handler post.
#See also
- Allow to enter particular characters only
- Attach event handlers inside other handlers
- Calculate the mouse position relative to an element
- Communication between an iframe and its parent window
- Copy highlighted code to the clipboard
- Count the number of characters of a textarea
- Create a custom scrollbar
- Create a range slider
- Create an image comparison slider
- Create resizable split views
- Detect if the caps lock is on
- Distinguish between left and right mouse clicks
- Drag and drop element in a list
- Drag and drop table column
- Drag and drop table row
- Drag to scroll
- Export a table to csv
- Get size of the selected file
- Get the size of an image
- Highlight an element when dragging a file over it
- Make a draggable element
- Make a resizable element
- Paste as plain text
- Placeholder for a contenteditable element
- Press shift and enter for a new line
- Prevent the default action of an event
- Print an image
- Replace broken images
- Resize an iframe to fit its content
- Resize an image
- Resize columns of a table
- Resize the width of a text box to fit its content automatically
- Select the text of a textarea automatically
- Show a custom context menu at clicked position
- Show a ghost element when dragging an element
- Show a loading indicator when an iframe is being loaded
- Show or hide table columns
- Sort a table by clicking its headers
- Zoom an image
Questions? 🙋
Do you have any questions about front-end development? If so, feel free to create a new issue on GitHub using the button below. I'm happy to help with any topic you'd like to learn more about, even beyond what's covered in this post.
While I have a long list of upcoming topics, I'm always eager to prioritize your questions and ideas for future content. Let's learn and grow together! Sharing knowledge is the best way to elevate ourselves 🥷.
Recent posts ⚡
Newsletter 🔔
If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.
Phước Nguyễn