/    Sign up×
Community /Pin to ProfileBookmark

Javascript if then syntax help…

How would I make a if then statement in a onclick function that basically says this:

if checkbox is set to checked and textbox is set to nothing…alert!

Any suggestions?

to post a comment
JavaScript

13 Comments(s)

Copy linkTweet thisAlerts:
@fredmvJan 11.2004 — <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>untitled</title>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=iso-8859-1" />
<script type="text/javascript">
//<![CDATA[
function foo()
{
var e = document.forms[0].elements;
if(e[0].checked && !e[1].value) alert('!');
}
//]]>
</script>
</head>
<body>

<i> </i> &lt;form action="#"&gt;
<i> </i> &lt;div&gt;
<i> </i> &lt;input type="checkbox" /&gt;
<i> </i> &lt;input type="text" /&gt;
<i> </i> &lt;hr /&gt;
<i> </i> &lt;input type="button" value="testa" onclick="foo();" /&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;/form&gt;
<i> </i>&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@tcovertauthorJan 11.2004 — What about this:

if checkbox1 is checked and textfield1 is empty then alert else do href

if checkbox2 is checked and textbox2 is empty then alert else do same href as checkbox1

This make sense? Basically if those don't alert then I want it to continue and do the href on the button.
Copy linkTweet thisAlerts:
@IsmAvatarJan 11.2004 — I'd assume that to be:

[code=php]
if(checkbox1.checked && !textbox1.value){alert('!')};
else{href}
if(checkbox2.checked && !textbox2.value){alert('!')};
else{href}
[/code]


but I've never seen an else statement used.
Copy linkTweet thisAlerts:
@tcovertauthorJan 11.2004 — That wouldn't work because if checkbox1 was good, but checkbox2 wasn't, it would still do the href.
Copy linkTweet thisAlerts:
@IsmAvatarJan 11.2004 — err, I thought that's what you asked for...

perhaps this?...
[code=php]
if(checkbox1.checked && !textbox1.value || checkbox2.checked && !textbox2.value){alert('!')};
else{href}
[/code]


replace || with whatever the or statement is.

[COLOR=indigo]EDIT in responce to below post.[/COLOR]
Copy linkTweet thisAlerts:
@tcovertauthorJan 11.2004 — That is closer...but, I would like to have a customized message for each checkbox that errors, instead of one message if they both error.

if checkbox1 is checked and textbox1 is empty alert

if checkbox2 is checked and textbox2 is empty then alert

if neither alert then do href
Copy linkTweet thisAlerts:
@IsmAvatarJan 11.2004 — OH, I see.

is there an elseif statement in javascript?

can someone else help, I'm just learning javascript, and I'm using my QBasic and other such languages knowledge.
Copy linkTweet thisAlerts:
@PittimannJan 11.2004 — Hi!
...I want it to continue and do the href on the button.[/QUOTE]Do the href on the button - what do you mean??

Guess, in that case you wanna go somewhere. Please try something like that:
<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled&lt;/title&gt;
&lt;script language="JavaScript" type="text/javascript"&gt;
&lt;!--
function check(){
if(document.myForm.checkbox1.checked &amp;&amp; document.myForm.textbox1.value==""){
alert('1!')
};
else if(document.myForm.checkbox2.checked &amp;&amp; document.myForm.textbox2.value==""){
alert('2!')
};
else{
window.location="myhref.htm";
}
}
//--&gt;
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form name="myForm"&gt;
&lt;input type="checkbox" name="checkbox1"&gt; &lt;input type="text" name="textbox1"&gt;&lt;br&gt;
&lt;input type="checkbox" name="checkbox2"&gt; &lt;input type="text" name="textbox2"&gt;&lt;br&gt;
&lt;input type="button" onclick="check()" value="check Form"&gt;
&lt;/body&gt;
&lt;/html&gt;

Cheers - Pit
Copy linkTweet thisAlerts:
@tcovertauthorJan 12.2004 — I tried this script, but it didn't do anything. It is like the check() function never even ran.

Is there something not right in the code?


Does it work for you?
Copy linkTweet thisAlerts:
@tcovertauthorJan 12.2004 — Nevermind. I found what the probloem was. At the end of each if then, the ";" needs to be inside the "}"....thanks a ton.
Copy linkTweet thisAlerts:
@PittimannJan 12.2004 — Hi!

Very sorry for that!! But glad you got it yourself :rolleyes:

Cheers - Pit
Copy linkTweet thisAlerts:
@tcovertauthorJan 12.2004 — Okay, this is weird.

It does what it is suppose to on my page, except after prompting the alert message for the textbox to be filled, and I press OK, it continues and submits. But, it doesn't go to the href that I sepcify, it goes to itself, and includes all of the field variables on the page in the URL.

Why is this?
Copy linkTweet thisAlerts:
@tcovertauthorJan 12.2004 — Nevermind, everytime I send another post, something clicks and I figure it out. Crazy.

But, here is something else. After I hit "OK" on the alert, it refreshes the page, and clears by checked box.

Is there anyway to have it just stop...no refresh?
×

Success!

Help @tcovert spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...