Vikas Thakur
Vikas Thakur

Reputation: 27

unable to call two functions on javascript onclick event

I want to call two javascript functions, for this i am using

<input type="submit" values="submit" onclick="return mandatoryCheck(); submitScore();">

But submitScore() function didn't execute. Need help...

Upvotes: 0

Views: 780

Answers (5)

tec
tec

Reputation: 1147

I had a similar problem, selectJQgridRow() was not working. My input was like:

<input type="button" values="Check" onclick="mandatoryCheck(); selectJQgridRow();">

The JavaScript functions were in a separated JavaScript file. The problem was in the functions definition time. I had to create selectJQgridRow() after the jQgrid table definition. I just put it the last and done.

Upvotes: 0

user3117575
user3117575

Reputation:

Using return inside of a function (in this case, the onclick is a function) will essential "stop" anything past the return statement to happen.

For instance:

var example = function(){
  return true;
  console.log("Hello.")
}

That will never log because the function returned a value before the other console.log() function could be reached...

That being said, you should redesign your function like this:

onclick="mandatoryCheck(); submitScore();"

Upvotes: 0

Satpal
Satpal

Reputation: 133403

Assuming your are performing some validation check in mandatoryCheck() and its returning true/false. You should use && operator

Use

<input type="submit" values="submit" onclick="return mandatoryCheck() && submitScore();">

Upvotes: 1

Josh Stevens
Josh Stevens

Reputation: 4221

Add semi-colons ; to the end of the function calls in order for them both to work.

<input type="submit" values="submit" onclick="mandatoryCheck(); submitScore();">

Here is a good reference from SitePoint http://reference.sitepoint.com/html/event-attributes/onclick

Upvotes: 0

galitskyd
galitskyd

Reputation: 216

The 2nd function is unreachable because of the return.

<input type="submit" values="submit" onclick="mandatoryCheck(); submitScore();">

Upvotes: 0

Related Questions