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

  • SEARCH
  • Home
  • 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 9274415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:20:54+00:00 2026-06-18T16:20:54+00:00

I’m using jQuery with it’s widget factory and using custom events to handle events

  • 0

I’m using jQuery with it’s widget factory and using custom events to handle events in my application.

This means that all my event binding looks a lot like:

//...in the widget factory code

$(this.element).addClass('customEventClass');
$(this.element).bind('mysite.loadNextPage', $.proxy(this, 'loadNextPage');

and the events are triggered by doing:

$('.customEventClass').trigger('mysite.loadNextPage');

Because the events are directly bound to the elements that need to receive them, I don’t need to have these custom events to bubble up through the DOM. I know I can check whether the even has bubbled up or not by doing this in the event handler code:

if (event.target != event.currentTarget) {
    event.stopPropagation();
    event.preventDefault();
    return;
}

But at the moment because most of the elements that are listening for the custom events don’t have a handler registered for ‘mysite.loadNextPage’ there are 51 events generated where only 1 actually does anything. Is there a way to either:

  1. tell jQuery not to bubble these events at all or
  2. Add a default ‘stop propagation’ handler to all DOM objects that have class ‘customEventClass’ to stop them from bubbling up an event that they don’t have a specific handler for.

Or are there any other good practices for only triggering events on elements that are interesting in those events, rather than having lots of events be triggered for elements that aren’t interested in those events.

  • 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-18T16:20:56+00:00Added an answer on June 18, 2026 at 4:20 pm

    It looks like there’s no good way to do it with jQuery as it is, but it’s very easy to write a new function to allow this.

    First I wrote a new function to stop the event from bubbling(, and also log the event why not).

    function    eventWrapper(event){
    
        var logString = 'Event called: ' + event.type + ":" + event.namespace;
    
        if (jQuery.isFunction(this.log) == true) {
            this.log(logString);
        }
        else if (jQuery.isFunction(Logger.log) == true) {
            Logger.log(logString);
        }
        else{
            console.log(logString);
        }
    
        event.stopPropagation();
    }
    

    And now a new function that is added to jQuery.

    // A global GUID counter for objects
    guidWrapper: 1,
    
    proxyWrapper: function(wrappedFn, fn, context, wrapFn ) {
    
        var args, proxy, tmp;
    
        if ( typeof context === "string" ) {
            tmp = fn[ context ];
            context = fn;
            fn = tmp;
        }
    
        // Quick check to determine if target is callable, in the spec
        // this throws a TypeError, but we will just return undefined.
        if ( !jQuery.isFunction( fn ) ) {
            return undefined;
        }
    
        // Simulated bind
        args = core_slice.call( arguments, 3 );
        proxy = function() {
            wrappedFn.apply( context || this, args.concat( core_slice.call( arguments ) ) );
            return fn.apply( context || this, args.concat( core_slice.call( arguments ) ) );
        };
    
        // Set the guid of unique handler to the same of original handler, so it can be removed
        proxy.guid = fn.guid = fn.guid || jQuery.guid++;
    
        return proxy;
    },
    

    And then instead of binding the function like this:

    $(this.element).bind('click', $.proxy(this.click, this));
    

    Instead bind it like this.

    $(this.element).bind('click', $.proxyWrapper(eventWrapper, this.click, this));
    

    This means that when the event is triggered, the first element that is listening for that event will call event.stopPropagation on the event, and so it won’t bubble up to other elements that may also be listening for that event.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I know there's a lot of other questions out there that deal with this
I am using Paperclip to handle profile photo uploads in my app. They upload
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I am reading a book about Javascript and jQuery and using one of the
I am using JSon response to parse title,date content and thumbnail images and place
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.