Solved

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

Posted on 2013-11-13
1
475 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
1 Comment
 
LVL 42

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now