Attach or detach an event handler
Written byPhuoc Nguyen
Category
Level 1 — Basic
Created
17 Feb, 2020
Last updated
18 Jun, 2020
#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: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
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? Not just about this specific post, but about any topic in front-end development that you'd like to learn more about? If so, feel free to send me a message on Twitter or send me an email. You can find them at the bottom of this page.
I have a long list of upcoming posts, but your questions or ideas for the next one will be my top priority. Let's learn together! Sharing knowledge is the best way to grow 🥷.
Recent posts ⚡
Copy the content of an element to your clipboard
01 Oct, 2023
Make a text area fit its content automatically
30 Sep, 2023
Quickly insert alternate characters while typing
30 Sep, 2023
Zebra-like background
30 Sep, 2023
Add autocomplete to your text area
28 Sep, 2023
Linear scale of a number between two ranges
28 Sep, 2023
Highlight the current line in a text area
27 Sep, 2023
Create your own custom cursor in a text area
27 Sep, 2023
Mirror a text area for improving user experience
26 Sep, 2023
Display the line numbers in a text area
24 Sep, 2023
Select a given line in a text area
24 Sep, 2023
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