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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:12:49+00:00 2026-05-23T12:12:49+00:00

In jQuery, I do stuff like this a lot: $(‘#myId’).bind(‘myevent’, function() { … }).trigger(‘myevent’);

  • 0

In jQuery, I do stuff like this a lot:

$('#myId').bind('myevent', function() { ... }).trigger('myevent');

Works great when the selector only finds one element, but I don’t want it to fire more than once if there’s more than one match.

Is there some way to basically declare an anonymous function and execute it exactly once all in one swoop?

Or do you have to make it non-anonymous (give it a name), pass it in, and call it once manually?


For example, I might bind a .change event to set of radio buttons. In the callback, I check which one is actually set and then show/hide a div. When the page loads, the div needs to be in the correct state, so I fire the event once and let the JS figure out whether or not it should be shown. But I don’t really want it to fire 3 times because I’ve got 3 radio buttons.

  • 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-05-23T12:12:50+00:00Added an answer on May 23, 2026 at 12:12 pm

    Use the triggerHandler()[docs] method instead of the trigger()[docs] method.

    This will invoke the handler only once, and will not allow the event to bubble or trigger the default behavior.

    From the docs:

    The .triggerHandler() method behaves
    similarly to .trigger(), with the
    following exceptions:

    • The .triggerHandler() method does not cause the default behavior of an
      event to occur (such as a form
      submission).
    • While .trigger() will operate on all elements matched by the jQuery
      object, .triggerHandler() only affects
      the first matched element.
    • Events created with .triggerHandler() do not bubble up the
      DOM hierarchy; if they are not handled
      by the target element directly, they
      do nothing.
    • Instead of returning the jQuery object (to allow chaining),
      .triggerHandler() returns whatever
      value was returned by the last handler
      it caused to be executed. If no
      handlers are triggered, it returns
      undefined
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In jQuery, I've done stuff like this in the past: $('#someCheckbox').click(); And everything works
Jquery has a great language construct that looks like this: $(document).ready(function() { $(a).click(function() {
Still learning this jQuery stuff.... I like this ajax page loader; it works for
I tried to do this: try{ (function($){ ... my plugin stuff })(jQuery); }catch(er){ alert(an
I generally use this in jquery $(document).ready(function() { //stuff } I was just on
I have a jquery block that does some ajax stuff like this: $.ajax({ url:
I know stuff like jQuery Mobile , which makes it a lot easier to
Hi this is pretty basic jQuery stuff but i am a little lost. So
This is a really newbie question. I've managed to get some jQuery stuff running
I'm really new at this jquery stuff. I have a wcf web service running

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.