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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:38:34+00:00 2026-06-09T02:38:34+00:00

I add a click event handler to an element $(.elem).click(function(){ $.post(page.php.function(){ //code1 }) })

  • 0

I add a click event handler to an element

 $(".elem").click(function(){
       $.post("page.php".function(){
         //code1
      })
 })

And then I trigger a click event

$(".elem").click();
//code2

How can i make sure that code2 executes after code1 executes

  • 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-09T02:38:36+00:00Added an answer on June 9, 2026 at 2:38 am

    (Ignoring WebWorkers) JavaScript runs on a single thread, so you can be sure that code2 will always execute after code1.

    Unless your code1 does something asynchronous like an Ajax call or a setTimeout(), in which case the triggered click handler will complete, then code2 will execute, then (eventually) the callback from the Ajax call (or setTimeout(), or whatever) will run.

    EDIT: For your updated question, code2 will always execute before code1, because as I said above an async Ajax callback will happen later (even if the Ajax response is very fast, it won’t call the callback until the current JS finishes).

    “How i make sure that code2 executes after code1 executes”

    Using .click() with no params is a shortcut to .trigger("click"), but if you actually call .trigger() explicitly you can provide additional parameters that will be passed to the handler, which lets you do this:

    $(".elem").click(function(e, callback) {
        $.post("page.php".function(){
          //code1
    
          if (typeof callback === "function")
             callback();
       });
    });
    
    $(".elem").trigger("click", function() {
        // code 2 here
    });
    

    That is, within the click handler test whether a function has been passed in the callback parameter and if so call it. This means when the event occurs “naturally” there will be no callback, but when you trigger it programmatically and pass a function then that function will be executed. (Note that the parameter you pass with .trigger() doesn’t have to be a function, it can be any type of data and you can pass more than one parameter, but for this purpose we want a function. See the .trigger() doco for more info.)

    Demo: http://jsfiddle.net/nnnnnn/ZbRJ7/1/

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

Sidebar

Related Questions

I can't seem to add a click event handler to the Dialog header/title. Something
To add a click event handler to an element, is .bind('click') or .click better?
How can I add click event to newly added item of menu? Below is
How can you add a click event to dynamically created menu item? I thought
I thought jQuery's click() can let us add a handler or just click on
SOLVED! $('#bset1').click(function(event) { event.stopPropagation(); }); I have several radio buttons in the element. Everything
I am trying to add a click event on the element that is return
Is there a way I can add additional buttons with click event handlers to
An event handler can be removed with the following code in the button click
Is it possible to add some kind of an event/handler when an element is

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.