Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-11-13
1
Medium Priority
?
513 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 44

Accepted Solution

by:
Chris Stanyon earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
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.
Suggested Courses

636 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