zarkoz
zarkoz

Reputation: 369

Multiple Functions for Single Event in JQuery

I have one function for one element:

<a href="javascript:void(0);" onclick="dothis()">Link</a>

When this function is done, how can I run another function after this one?

For example, if this function returns true, then the script will run another function and if not, it will not run the second function.

Or have the second function run without regard to the result of the first function.

Any help appreciated :)

Upvotes: 0

Views: 511

Answers (6)

11684
11684

Reputation: 7517

In the onclick attribute, you can use normal javascript, socalled inline javascript. So you could do (disencouraged):

<a onclick="if(dothis()) dothat()" />

You could do (recommended):

<a onclick="dothisandthat()" />

<script>
    function dothisandthat() {
        if (dothis())
            dothat();
</script>

Upvotes: 2

Nicole
Nicole

Reputation: 33227

The most concise way:

doThis() && doThat();

jQuery:

$('#el').click(function() {
    doThis() && doThat();
});

HTML:

<a href="javascript://" onclick="doThis() && doThat();">Link</a>

Upvotes: 2

casraf
casraf

Reputation: 21694

<a href="javascript:void(0);" onclick="if (dothis() == true) dothat();">Clicky</a>

Upvotes: 3

Robert
Robert

Reputation: 1907

You can simply call another function from dothis()

 function dothis(){

  // Do something

    if(val == true)
    {
     // Call another function
      anotherFunction();
    }

 }

Upvotes: 2

Prasenjit Kumar Nag
Prasenjit Kumar Nag

Reputation: 13471

You can just call that another function at the end of dothis function like this

function dothis(){

  if(someThing == true)
    someFunc();
}

Upvotes: 0

Mohsen
Mohsen

Reputation: 65845

call the other function at end of dothis

If you want to use dothis with other elements. simply use a callback function.

function dothis(callback) {

 // code

 callback();

}

then use it like this:

dothis(dothisafter);

Upvotes: 0

Related Questions