Solved

jQuery: How do I remove an .on(...) event handler correctly?

Posted on 2013-11-13
1
489 Views
Last Modified: 2013-11-13
I can remove an (object selector).on('focus',..) event handler by using (object selector).off('focus') but of course if there is another 'focus' event handler that will get removed as well.

Is there a way to just remove the event handler I added and not remove any others?
0
Comment
Question by:purplesoup
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39644436
You would need to use a named function rather than an anonymous function when binding the focus event. Normally, you use an anonymous function:

$('#someElement').on('focus', function(e) { alert("Focused") });

Open in new window

Instead, create an eventHandler and use that:

//The eventHandler
function myFocusEvent(e) { alert("Focused") }

//Bind the focus event to the eventHandler
$('#someElement).on('focus', myFocusEvent);

Open in new window

You can then specifically remove that eventHandler:

$('#someElement').off('focus', myFocusEvent);

Open in new window

0

Featured Post

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question