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 8198349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:55:59+00:00 2026-06-07T05:55:59+00:00

I find myself often needing to bind an event handler, as well as immediately

  • 0

I find myself often needing to bind an event handler, as well as immediately execute that handler in jQuery. To date, I’ve been using this lazy faux-pattern to keep the code DRY:

$(...).bind('event', function() {
    ...
}).trigger('event');

Today I was bitten by this, due to the fact that other handlers had already been bound to event, and they were also executed when I triggered the event in this way, but caused a lot of problems, since they weren’t supposed to have been executed at that time.

To resolve the problem, I’ve changed the pattern to this:

var handler = function() {
    ...
};

var element = $(...);

element.bind('event', handler);
handler.call(element);

This works the way I expect, but it’s ugly as sin and quite verbose, so I’m considering encapsulating this pattern into a jQuery plugin:

$(...).bindAndExecute('event', function() { ... });

I could find no equivalent for this in jQuery.bind() options or any of the other methods, but I may have missed something. Is there a more concise way of doing this that I haven’t thought of? I don’t want to reinvent the wheel.

  • 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-07T05:56:00+00:00Added an answer on June 7, 2026 at 5:56 am

    jQuery supports namespacing your events. This is a very useful technique and comes quite handy when you want unbind or trigger a specific group of events.

    $(...).bind('event.myNamespace', function() {
        ...
    }).trigger('event.myNamespace');
    

    Using this technique is almost unavoidable (or at least sensible people won’t avoid it) when you are developing bigger applications or jQuery plugins.

    Quick jsFiddle Demo

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

Sidebar

Related Questions

I often find myself needing reference to an object that is several objects away,
I often find myself needing a tool that would allow me to: search for
I find myself often needing to use int.TryParse() to test if a value is
I often find myself wanting to debug CSS layout issues that involve DOM changes
I often find myself with a list of disconnected Linq2Sql objects or keys that
I often find myself with a file that has one number per line. I
I often find myself wanting to just print (using the implicit toString() of each
When I write SQL queries, I find myself often thinking that there's no way
I find myself very often creating an object that has no public methods and
I find myself very often in the situation that I open an element in

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.