473,405 Members | 2,154 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,405 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 28792
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?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.