/    Sign up×
Community /Pin to ProfileBookmark

Using the Case statement

Hi I’m trying to add another variable to this code using the Case statement:

[QUOTE]

<script>
function convert() {
var first = document.getElementsByName( “first”)[0],
second = document.getElementsByName( “second”)[0];

first.value = first.value.replace( /s/g, “”);

second.value = “Nice “+ first.value +” color”;

switch(first.value){
case ‘blue’:
case ‘green’:
case ‘red’:
return true;
break;
default:
return confirm(second.value + “?nnAre you serious?”);
break;
}

}
</script>

<form name= “execute” onsubmit=’return convert();’>
<input name=’first’ type=’text’ /> <input name=’second’ type=’hidden’ />
<input type=’submit’ value=”Execute” />
</form>

[/QUOTE]

So for instance if you typed in ‘purple’ and only ‘purple’ it submits:

[QUOTE]

second.value = “Horrible “+ first.value +” color!”;

[/QUOTE]

Wherever I put this code it doesn’t work. I’ve also added this before the default:

[QUOTE]

break;
case ‘purple’:
return true;
break;

[/QUOTE]

It’s probably something really simple I’m missing. Any help would be great.

Thanks

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@cgishackFeb 09.2008 — Humm.. I am not sure if i understand completely,, but try this..
<i>
</i>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;

&lt;script&gt;
function convert()
{
var first = document.getElementsByName( "first")[0],
second = document.getElementsByName( "second")[0];

<i> </i>first.value = first.value.replace( /s/g, "");
<i> </i>second.value = "Nice "+ first.value +" color";

<i> </i>var willSubmit = false;

<i> </i> switch(first.value)
<i> </i> {
<i> </i> case 'blue':
<i> </i> break;
<i> </i> case 'green':
<i> </i> break;
<i> </i> case 'red':
<i> </i> break;
<i> </i> case 'purple':
<i> </i> willSubmit = true;
<i> </i> break;
<i> </i> default:
<i> </i> return confirm(second.value + "?nnAre you serious?");
<i> </i> break;
<i> </i> }
<i> </i> return willSubmit;
}
&lt;/script&gt;

&lt;form name= "execute" onsubmit='return convert();'&gt;
&lt;input name='first' type='text' /&gt; &lt;input name='second' type='hidden' /&gt;
&lt;input type='submit' value="Execute" /&gt;
&lt;/form&gt;



&lt;/body&gt;
&lt;/html&gt;


Drew
Copy linkTweet thisAlerts:
@iGirlauthorFeb 09.2008 — Thanks for the quick response. All I want the form to do is submit 'Nice (blue, green or red) color' when 'blue', 'green' or 'red' are typed in and 'Horrible (purple) color!' only when 'purple' is typed in. Anything else must prompt the Confirm and submit the form on 'ok'. Thanks again.
Copy linkTweet thisAlerts:
@cgishackFeb 09.2008 — Ok, i understand,

Try this..
<i>
</i>
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;

&lt;script&gt;
function convert()
{
var first = document.getElementsByName( "first")[0],
second = document.getElementsByName( "second")[0];

<i> </i>first.value = first.value.replace( /s/g, "");
<i> </i>second.value = "Nice "+ first.value +" color";
<i> </i>var response = true;
<i> </i> switch(first.value)
<i> </i> {
<i> </i> case 'blue':
<i> </i> break;
<i> </i> case 'green':
<i> </i> break;
<i> </i> case 'red':
<i> </i> break;
<i> </i> case 'purple':
<i> </i> second.value = "Horrible "+ first.value +" color!";
<i> </i> break;
<i> </i> default:
<i> </i> response = confirm(second.value + "?nnAre you serious?");
<i> </i> break;
<i> </i> }
<i> </i> //check if the use clicked cancel
<i> </i> if (response == false)
<i> </i> {
<i> </i> return false;
<i> </i> }
}
&lt;/script&gt;

&lt;form name= "execute" onsubmit='return convert();'&gt;
&lt;input name='first' type='text' /&gt; &lt;input name='second' type='hidden' /&gt;
&lt;input type='submit' value="Execute" /&gt;
&lt;/form&gt;



&lt;/body&gt;
&lt;/html&gt;


Drew
Copy linkTweet thisAlerts:
@iGirlauthorFeb 09.2008 — It works! Thanks so much Drew!
×

Success!

Help @iGirl 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 5.5,
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,
)...