WeLoveCSS Logo
Home Profile Members Search Rules Help New Posts

WeLoveCSS > PROGRAMMING LANGUAGES > Scripting and Server Side > Switch...where am I going wrong here..

Reply
  Thread Tools Display Modes
Old 8th February 10, 07:38 PM   #1
steve
WLC Member
 
Join Date: Jan 2010
Posts: 28
Default Switch...where am I going wrong here..

Hi Guys, I'm back... I'm going to get this Javascript stuff figured out yet!

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/Transitional.dtd">
<html>
<head>
<title>Switch Statment</title>

<script language="javascript" type="text/javascript">

grade = document.frmOne.gradeScore.value
switch(grade){
case '1': alert('Congratulations! Your Grade is an "A!"');break;
case '2': alert('Your Grade is a "B"');break;
case '3': alert('Your Grade is a "C"');break;
case '4': alert('Your Grade is a "D"');break;
case '5': alert('Your Grade is a "F"');break;
case '6': alert('Your Grade is a "G"');break;
default:break;
} 

</script>
</head>
<body>
<form name="frmOne" id="frmOne">
<select name="gradeScore">
<option value="">Please Select Your Score</option>
<option value="1">90 to 100</option>
<option value="2">80 to 89</option>
<option value="3">70 to 79</option>
<option value="4">60 to 69</option>
<option value="5">50 to 59</option>
<option value="6">Below 50</option>
</select></form>
</body>
</html>

Last edited by meesa; 8th February 10 at 07:56 PM. Reason: Added [code] Tags
steve is offline   Reply With Quote
Old 8th February 10, 07:56 PM   #2
meesa
WLC Mod
 
meesa's Avatar
 
Join Date: Jul 2009
Location: Milky Way Galaxy
Posts: 1,602
Default Re: Switch...where am I going wrong here..

You're running your code before the form loads. Put the JS after the form and it will work.
__________________
Romans 3:23 KJV - For all have sinned, and come short of the glory of God;
Romans 6:23 KJV - For the wages of sin is death; but the gift of God is eternal life through Jesus Christ our Lord.
Do you have the gift of God? I do!
meesa is offline   Reply With Quote
Old 8th February 10, 08:06 PM   #3
steve
WLC Member
 
Join Date: Jan 2010
Posts: 28
Default Re: Switch...where am I going wrong here..

I tried this and it didn't work...where am I going wrong this time?
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/Transitional.dtd">
<html>
<head>
<title>Switch Statment</title>


</head>
<body>
<form name="frmOne" id="frmOne">
<select name="gradeScore">
<option value="">Please Select Your Score</option>
<option value="1">90 to 100</option>
<option value="2">80 to 89</option>
<option value="3">70 to 79</option>
<option value="4">60 to 69</option>
<option value="5">50 to 59</option>
<option value="6">Below 50</option>
</select></form>
<script language="javascript" type="text/javascript">

grade = document.frmOne.gradeScore.value;
switch(grade){
case '1': alert('Congratulations! Your Grade is an "A!"');break;
case '2': alert('Your Grade is a "B"');break;
case '3': alert('Your Grade is a "C"');break;
case '4': alert('Your Grade is a "D"');break;
case '5': alert('Your Grade is a "F"');break;
case '6': alert('Your Grade is a "G"');break;
default:break;
} 


</script>
</body>
</html>

Last edited by meesa; 8th February 10 at 08:19 PM. Reason: Added [code] Tags
steve is offline   Reply With Quote
Old 8th February 10, 08:20 PM   #4
meesa
WLC Mod
 
meesa's Avatar
 
Join Date: Jul 2009
Location: Milky Way Galaxy
Posts: 1,602
Default Re: Switch...where am I going wrong here..

What is it doing?
__________________
Romans 3:23 KJV - For all have sinned, and come short of the glory of God;
Romans 6:23 KJV - For the wages of sin is death; but the gift of God is eternal life through Jesus Christ our Lord.
Do you have the gift of God? I do!
meesa is offline   Reply With Quote
Old 8th February 10, 08:27 PM   #5
steve
WLC Member
 
Join Date: Jan 2010
Posts: 28
Default Re: Switch...where am I going wrong here..

The form shows up, but it doesn't react to when I select a value
steve is offline   Reply With Quote
Old 8th February 10, 08:38 PM   #6
meesa
WLC Mod
 
meesa's Avatar
 
Join Date: Jul 2009
Location: Milky Way Galaxy
Posts: 1,602
Default Re: Switch...where am I going wrong here..

Try alerting grade. Make sure it's getting the value.
__________________
Romans 3:23 KJV - For all have sinned, and come short of the glory of God;
Romans 6:23 KJV - For the wages of sin is death; but the gift of God is eternal life through Jesus Christ our Lord.
Do you have the gift of God? I do!
meesa is offline   Reply With Quote
Old 9th February 10, 02:14 PM   #7
james342
WLC Member
 
Join Date: Jan 2009
Posts: 7
Default Re: Switch...where am I going wrong here..

You need to run the script when the select changes so something like this should do.

HTML Code:
<select name="gradeScore" onchange="findGrade(this.value)">
...
</select>

<script language="javascript" type="text/javascript">

function findGrade(grade){

switch(grade){
case '1': alert('Congratulations! Your Grade is an "A!"');break;
...
default:break;
} 
}
</script>
You need to put your script in a function so it can be called as a function. You then need to pass the value of the select to the function when the value is changed.

Alternatively you could use

HTML Code:
<select onchange="alert(this.value)">
<option value='Congratulations! Your Grade is an "A!"'>90 to 100</option>
<option value='Your Grade is a "B"'>80 to 89</option>
<option value='Your Grade is a "C"'>70 to 79</option>
<option value='Your Grade is a "D"'>60 to 69</option>
<option value='Your Grade is a "F"'>50 to 59</option>
<option value='Your Grade is a "G"'>Below 50</option>
</select>
You would not need the javascript function at all.

However this may mess up any form handling that you have in place
__________________

To view links or images in signatures your post count must be 5 or greater. You currently have 0 posts.
james342 is offline   Reply With Quote
Reply


Thread Tools
Display Modes
Linear Mode Linear Mode

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 09:30 PM.



Home | Advertise | Contact Us | Top
Home | Advertise | Contact Us | Top

Copyright© 2006 WeLoveCSS.com. All Rights Reserved. Designed by DESIGNGRAPHY
Powered by vBulletin Version 3.8.4 Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.