473,221 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

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 28770
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Michel | last post by:
Hi Group of helpers, Following snippet of code: <HTML> <HEAD> <script> function color(value) { alert(value);
17
by: dan_williams | last post by:
I have the following test web page:- <html> <head><title>Test</title> <script language="Javascript"> <!-- function fnTR() { alert("TR"); }
35
by: Logos | last post by:
I asked about this a while ago, and got a great answer and a reference to http://www.javascripttoolbox.com/bestpractices/new.php. I just need to override onclick and return false. No biggie! ...
8
by: hobosalesman | last post by:
Consider the following (where 'a' is an anchor element with a valid URL href): a.onclick=help_mode; function help_mode() { alert("hi"); return false; } As expected clicking on the link now...
13
by: alvin.yk | last post by:
Hi, Normally, a piece of code such as <a href="http://www.yahoo.com" onclick="alert('hello');return false;">link</a> will stop the browser from actually going to href's destination....
1
b1randon
by: b1randon | last post by:
First off, from the title my first thing to say would be that someone was probably assigning a function with parameters (which won't work). That's no the case. I'm using function (){ //dynamically...
3
by: lewnussi | last post by:
I am trying to call more than one function starting with onclick in a form to work in all browsers. In the form, this calls only function_1: <input type="submit" value="thevalue"...
18
by: navyjax2 | last post by:
What if your event handler has to be nonstatic?
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.