Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9001259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:24:45+00:00 2026-06-16T00:24:45+00:00

I want some way to add and remove unique, named events to elements so

  • 0

I want some way to add and remove unique, named events to elements so I can make sure the event is never added more than once. When you add events to an element, they get stacked on each other, even if two of them are meant to be the same event. For a simple -but abstracted- example:

$('a').on("click", function(e){ console.log("Hi One"); }); // 1
$('a').on("click", function(e){ console.log("Hi One"); }); // duplicate
$('a').on("click", function(e){ console.log("Hi Two"); }); // 2
// Output will be: Hi One, Hi One, Hi Two

I tried using a method like .off().on(), but that only allows for one event at a time.

$('a').off("click").on("click", function(e){ console.log("Hi One"); }); 
$('a').off("click").on("click", function(e){ console.log("Hi One"); }); 
$('a').off("click").on("click", function(e){ console.log("Hi Two"); });
// Output will be: Hi Two

So I came up with this extension for jQuery:

$.fn.onUnique = function (
    /* uniqueEventName, events [,selector] [,data], handler(eventObject) */
) {
    var uniqueEventNameArray    = this.data("uniqueEvents") || [];
    var uniqueEventName         = arguments[0];
    if ($.inArray(uniqueEventName, uniqueEventNameArray) == -1) {
        uniqueEventNameArray.push(uniqueEventName);
        this.data("uniqueEvents", uniqueEventNameArray);
        this.on(arguments[1], arguments[2], arguments[3], arguments[4]);
    }
};

So I can get the effect I want…

$('a').onUnique("Hi1", "click", function(e){ console.log("Hi One"); }); 
$('a').onUnique("Hi1", "click", function(e){ console.log("Hi One"); }); 
$('a').onUnique("Hi2", "click", function(e){ console.log("Hi Two"); });
// Output will be: Hi One, Hi Two

My questions are two:

  1. Is there a better way to do this?
  2. How would I do the equivalent of .offUnique(), which would remove only the specified event (by name)?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:24:45+00:00Added an answer on June 16, 2026 at 12:24 am

    Check out the namespaced events : event.namespace

    $('a').off('click.hi1').on('click.hi1', function(e){ console.log("Hi One"); });
    $('a').off('click.hi1').on('click.hi1', function(e){ console.log("Hi One"); });
    $('a').off('click.hi2').on('click.hi2', function(e){ console.log("Hi Two"); }); 
    

    Demo http://jsfiddle.net/gaby/378rP/


    You should probably, though, try to organize your code better so you do not apply the same event twice..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I'm sure there is some way of doing what I want, but maybe
Is there some way to run 2 threads at the same time? I want
I want to export some classes, say, Dog and Cat . One way to
I want some like this: How make it ideologically correct?
It is possible to add and remove elements from an enum in Java at
I've been trying to figure out some layout update xml directives to add, remove
Is there a good way to add/remove a neuron and its associated connections into/from
I have created a table and some buttons to remove/add rows. Problem is, when
I want some examples of C preprocessor directives, such as: #define pi 3.14 #define
I want some code to be triggered every second. Usually, I'd create a Timer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.