0

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 :)

5
  • Just call the function after that function. If you are already using jQuery, I recommend to use it also for binding the event handler. Commented Jun 21, 2012 at 19:43
  • What have you tried? Commented Jun 21, 2012 at 19:44
  • Like, can't the last of dothis() be dothat();? Commented Jun 21, 2012 at 19:44
  • 1
    Duplicate of all of these: stackoverflow.com/… Commented Jun 21, 2012 at 19:45
  • wait, you're not even using jQuery at all! Commented Jun 21, 2012 at 20:37

6 Answers 6

3
<a href="javascript:void(0);" onclick="if (dothis() == true) dothat();">Clicky</a>
Sign up to request clarification or add additional context in comments.

4 Comments

Better: if (dothis()) dothat();.
This is what I could use thanks :) It's easy to add another function inside original one, but I had something else in mind
@FelixKling Yeah, true, but unlike PHP, JS always confuses me as to what it considers a "true" statement... I sometimes can't check for a null without it breaking!
Yeah, this can be confusing. But if we assume that the function returns true then there is really no reason to compare it to true.
2

You can simply call another function from dothis()

 function dothis(){

  // Do something

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

 }

Comments

2

The most concise way:

doThis() && doThat();

jQuery:

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

HTML:

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

Comments

2

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>

5 Comments

Why is that not recommended? Maybe he wants to only use the second function in one specific link while all others do not share the same logic. Naturally he /could/ use a parameter (maybe a boolean) to determine which function call would trigger the second one, but seeins as this is unneeded and takes more space, is that really necessary?
Inline javascript should always be kept short. @OhMrBigshot
One function is still readable, but putting an if-statement in there doesn't help. Things get interesting when adding an else-clause.
I'm not sure that's a real problem, I thinkan if and else is ok. I've seen IP.Board do worse!
That could be server generated. You don't know how that looks like in PHP. that could be something like: <el onclick="<? echo $scriptToDoSomething ?>" />. That stays readable, and the scripts can even be in an included (or required) PHP file.
0

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);

Comments

0

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

function dothis(){

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.