469,271 Members | 1,776 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Two Functions OnClick - Stop if First is False?

I have two functions on buttons onclick, they work but if the first
fails the second still runs. How can I prevent this? Simplified Code
below. Thanks
Function Function1 () {
if (something) {
return false
}

Function Function2 () {
if (something) {
return false
}

onclick="Function1(); Function2()"

May 10 '06 #1
8 28063
VK

Barkster wrote:
I have two functions on buttons onclick, they work but if the first
fails the second still runs. How can I prevent this? Simplified Code
below. Thanks
Function Function1 () {
if (something) {
return false
}

Function Function2 () {
if (something) {
return false
}

onclick="Function1(); Function2()"


Function Function1 () {
if (something) {
return false;
}
else {
return Function2();
}
}

Function Function2 () {
// do stuff
return false;
}

onclick="return Function1()"

May 10 '06 #2
tim

VK wrote:
Barkster wrote:
I have two functions on buttons onclick, they work but if the first
fails the second still runs. How can I prevent this? Simplified Code
below. Thanks
Function Function1 () {
if (something) {
return false
}

Function Function2 () {
if (something) {
return false
}

onclick="Function1(); Function2()"


Function Function1 () {
if (something) {
return false;
}
else {
return Function2();
}
}

Function Function2 () {
// do stuff
return false;
}

onclick="return Function1()"


Or

onclick= "if( Function(1) ) {Function2();}"

Tim

May 10 '06 #3
"Barkster" <bd***@hotmail.com> writes:
I have two functions on buttons onclick, they work but if the first
fails the second still runs.
Yes, nothing in the code to prevent that.
How can I prevent this?


Do you also want the click event to be cancelled if either returns
false?

onclick="if (Function1() !== false) { Function2(); }"

or less strict about the return value of Function1 (the above
counts only exactly the value "false" as preventing Function2):

onclick="return Function1() && Function2();"

It also cancels the click event if Function1 returns false or
it returns a true-equivalent value and Function2 returns false.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
May 10 '06 #4
Thanks, I'll try it that way again. I couldn't get it to work that way
for me. I think I may be confused about putting return in from of the
function call in the onclick. Mine work without it, can someone tell
me when to/not to put return in front of the function call? Thanks

May 10 '06 #5
VK said the following on 5/9/2006 5:58 PM:
Barkster wrote:
I have two functions on buttons onclick, they work but if the first
fails the second still runs. How can I prevent this? Simplified Code
below. Thanks
Function Function1 () {
if (something) {
return false
}

Function Function2 () {
if (something) {
return false
}

onclick="Function1(); Function2()"


Function Function1 () {


Function Function1()? The JS099 class is that way ====>

function Function1() might not produce as many errors.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 10 '06 #6
ASM
Barkster a écrit :
I have two functions on buttons onclick, they work but if the first
fails the second still runs. How can I prevent this? Simplified Code
below. Thanks
Function Function1 () {
if (something) {
return false
}

Function Function2 () {
if (something) {
return false
}

onclick="Function1(); Function2()"


Soluce 1 : onclick="if(!Function1()) Function2();"

or : onclick="Function1();"
with :
function Function1 () {
if (something) return false;
else Function2();
}


Soluce 2 : onclick="if(Function1()) Function2();"

example :
function Function1() {
return confirm('do you agree to continue?');
}
function Function2() {
if (confirm('Did it work?')) alert('Fine !');
}

--
Stephane Moriaux et son [moins] vieux Mac
May 10 '06 #7
Barkster wrote:
I have two functions on buttons onclick, they work but if
the first fails the second still runs. How can I prevent
this? Simplified Code below. Thanks

Function Function1 () { ^^^^^^^^
Should be 'function' in case sensitive javascript. Also, it is
conventional to use an initial uppercase letter in a function name to
denote a function acting in the role of a constructor (as the language
contains no inherent indicator of that condition) so it is best not to
use initial capitals with non-constructors.
if (something) {
return false
}
As there is no - else { return true; } - here when something is false
the Undefined value will be returned, and undefined type-converts to
false so may have the same effect as returning false in some contexts.

<snip> onclick="Function1(); Function2()"


If the functions return true if they do not return false you can do:-

onclick="return (Function1() && Function2());"

- so that if the first returns false the second is never tried.

Note the addition of the return statement within the onclick code, this
is based upon the assumption that you would want to cancel the onclick
action if either function returned false. If that is not required it can
be omitted.

Richard.
May 10 '06 #8
Thanks for all the info, very helpful.

Barclay

May 10 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Michel | last post: by
17 posts views Thread by dan_williams | last post: by
8 posts views Thread by hobosalesman | last post: by
18 posts views Thread by navyjax2 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.