1

I have a JSP page containing an href link. When the link is clicked, I need to call 2 javascript functions. The second javascript function should execute only if first function returns true. I have tried this:

 <a href="#" onClick="return firstFunction(); secondFunction()"> Click me </a>

But this is not working. All I want is that a second function gets executed if the first function returns true.

5 Answers 5

3

You can do that like this

<a href="#" onClick="return firstFunction() ? secondFunction() : false"> Click me </a>
Sign up to request clarification or add additional context in comments.

Comments

3

When you return the result of your first function the block is exited. Unless you're going to do something with the result, you should say:

<a href="#" onClick="return firstFunction() ? secondFunction() : false;">Click me</a>

Comments

2

You currently have:

function () {
    return firstFunction();
    secondFunction();
}

This calls firstFunction then returns its value, and never reaches the call to secondFunction.

You want:

function () {
    var result = firstFunction();
    if (result) {
        secondFunction();
    }
}

2 Comments

If firstFunction returns 1 then...?
Then it will have returned a true value.
1
<a href="#" onClick="foo()"> Click me </a>

function foo(){
    if (firstFunction() === true)
        secondFunction()
}

return exit the function so you just returned firstFunction and exited the function, like

function foo(){
    return firstFunction();
     secondFunction(); // Unreachable code.
}

7 Comments

@popnoodles, I don't count lines, just like the interpreter. Code is being judged by it's readability and efficiency, not by it's length.
So this done in one line is less readable than that function which is going to be outside of the line it's being called from which would therefore require it to be sought? And how is foo() descriptive?
@popnoodles, nothing should be in HTML anyway! javascript shouldn't be a part of the markup. So he should use a function anyway, and use an event listener.
I would put it in markup but I respect other peoples' ways of coding. OP has written it inline therefore when a solution exists that is perfectly valid it should be answered in the style OP has used.
Out of interest where is it dictated that "nothing should be in HTML anyway"?
|
1
<a href="#" onClick="return if (firstFunction()) secondFunction();"> Click me </a>

if you want true to mean "only true and not just something that = true using a boolean comparison"

<a href="#" onClick="return if (firstFunction()===true) secondFunction();"> Click me </a>

but you probably don't

3 Comments

If firstFunction returns 1 then...?
then it's true and will call secondFunction() ...?
But 1 isn't true, it's a truthy value, not true. just like 0 isn't false.

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.