Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call an action after click() in Jquery?

I want to load an image and some other actions after I click a certain DOM element, but I want to load them AFTER the clicking action finished.

Here is a code example:

 $("#message_link").click(function(){    if (some_conditions...){        $("#header").append("<div><img alt=\"Loader\"src=\"/images/ajax-loader.gif\"  /></div>");    }  }); 

The problem is that the if condition executes before the click action have finished(Or at least that is my impression). I need that the If condition executes after the click action has finished. Can some one please tell me a solution?

Thanks :)

like image 722
rogeliog Avatar asked Jun 26 '11 18:06

rogeliog


People also ask

How can call Click event on page load in jQuery?

$("document"). ready(function() { setTimeout(function() { $("ul. galleria li:first-child img"). trigger('click'); },10); });

What is Click () method?

The click() method simulates a mouse-click on an element. This method can be used to execute a click on an element as if the user manually clicked on it.

What is the difference between .click and .on (' click in jQuery?

So onclick creates an attribute within the binded HTML tag, using a string which is linked to a function. Whereas . click binds the function itself to the property element.


1 Answers

setTimeout may help out here

$("#message_link").click(function(){    setTimeout(function() {        if (some_conditions...){            $("#header").append("<div><img alt=\"Loader\"src=\"/images/ajax-loader.gif\"  /></div>");        }    }, 100); }); 

That will cause the div to be appended ~100ms after the click event occurs, if some_conditions are met.

like image 86
Kevin Decker Avatar answered Oct 04 '22 12:10

Kevin Decker