Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-15-2010, 04:28 PM   PM User | #1
nico_icecold
New Coder

 
Join Date: Dec 2008
Posts: 23
Thanks: 6
Thanked 0 Times in 0 Posts
nico_icecold is an unknown quantity at this point
If..else..if..else Statements using Form Values as conditions

Hey all,

Okay, Here is what i would like to do.
I have a calculator here:
http://www.nico-online.co.uk/calculator/calc1.html


which after some help from Dormilich - got working!

Now, I would like to set up some conditions, so if the use selected 'Mountainbike' then, the script will perform a different calculation to if they select the 'Rennrad' option.

Okay, so I was thinking an If..else..if..else statement would probably be best after reading up

This is what I have come up with:

Code:
<script language="JavaScript">
function Framesize()
{
var a = parseFloat(BikeSizer.Insleg.value);
if(BikeSizer.Biketype.value = "Mountainbike")
{
b = (a * 0.572);
c = Math.round(b); 
BikeSizer.FramesizeCM.value = c;

d = (a * 0.226);
e = Math.round(d);
BikeSizer.FramesizeInch.value = e;
}


else if (BikeSizer.Biketype.value = "Trekking-, Reise- oder Cityrad")
{
b = (a * 5);
c = Math.round(b); 
BikeSizer.FramesizeCM.value = c;

d = (a * 5);
e = Math.round(d);
BikeSizer.FramesizeInch.value = e;
	}
	
else{
	
	b = (a * 10);
c = Math.round(b); 
BikeSizer.FramesizeCM.value = c;

d = (a * 10);
e = Math.round(d);
BikeSizer.FramesizeInch.value = e;
	}

}
</script>
So, I'm not too sure if I can set the Menu values as recongnisable values!

Here are the values I have:

<select name="Biketype" style="font-size:11px;">
<option>Mountainbike</option>
<option>Trekking-, Reise- oder Cityrad</option>
<option>Rennrad</option>
</select>


Anyone out there offer any insight? Am I going horribly wrong somewhere?

Apologies for the stupidness , I am quite new to JS!

Thanks in advance
nico_icecold is offline   Reply With Quote
Old 02-15-2010, 05:11 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
This should move you forward. You can assign multiple attributes (such as price or framesize) to each option, thus:-


Code:
<form name = "myform">
<select name = "Biketype" style="font-size:11px;" onchange = "getDetails()">
<option value = "" price = "0">Select a bike type</option>
<option value = "Mountainbike" price = "199">Mountainbike</option>
<option value = "Trekking" price = "299">Trekking-, Reise- oder Cityrad</option>
<option value = "Rennrad" price = "149">Rennrad</option>
</select>
</form>

<script type = "text/javascript">
function getDetails() {

var a = document.myform.Biketype.options[document.myform.Biketype.selectedIndex].value;
var b = document.myform.Biketype.options[document.myform.Biketype.selectedIndex].price;
if (a !="") {
alert (a + " " + b);
// then carry out whatever further calculations needed based on price or whatever
}
}
</script>
Note the syntax - document.formname.elementname.value
so e.g.
var a = parseFloat(document.BikeSizer.Insleg.value);


<script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.



BTW, the time to say "thanks" is afterwards, not beforehand which gives the - doubtless unintended - impression that you take other people's voluntary unpaid assistance for granted. Or as British politician Neil Kinnock put it, "Don't belch before you have had the meal." Prefer to use "please" beforehand and if you find a response helpful then you can use the "Thank User For This Post" button.

A historian and a psychologist are sitting outside at a nudist colony. Historian: "Have you read Marx?" Psychologist: "Yes, I think they're from the wicker chairs."

Last edited by Philip M; 02-15-2010 at 05:27 PM..
Philip M is offline   Reply With Quote
Old 02-15-2010, 05:32 PM   PM User | #3
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,763
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Lightbulb Alternative ...

Alternative code since 'Philip M' beat me AGAIN!
Code:
<html>
<head>
<title>Switch Calcs</title>
<script type="text/javascript">
function CalcStuff(act) {
  var a = parseFloat(BikeSizer.Insleg.value); 
  switch (act) {
   case '1' :	// Mountainbike
     b = Math.round(a * 0.572);
     c = Math.round(a * 0.226);
     break;
   case '2' :	// Trekking-, Reise- oder Cityrad
     b = Math.round(a * 5);
     c = Math.round(a * 5);
     break;
   case '3' :	// Rennrad
     b = Math.round(a * 10);
     c = Math.round(a * 10);
     break;
   default : alert('Invalid selection');  break;
  }
  document.BikeSizer.FramesizeCM.value = b; 
  document.BikeSizer.FramesizeInch.value = c;
}
</script>

</script>
</head>
<body>
<form name="BikeSizer" onsubmit="return false">
<select name="Biketype" onchange="CalcStuff(this.value)" style="font-size:11px;">
<option value="0">Select</option>
<option value="1">Mountainbike</option>
<option value="2">Trekking-, Reise- oder Cityrad</option>
<option value="3">Rennrad</option>
</select>
<input type="text" name="FramesizeCM">
<br>
<input type="text" name="FramesizeInch">
</form>
</body>

</htm>
Untested, but should be close.
Good Luck!

Last edited by jmrker; 02-15-2010 at 05:36 PM..
jmrker is offline   Reply With Quote
Users who have thanked jmrker for this post:
nico_icecold (02-16-2010)
Old 02-15-2010, 06:50 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Something wrong here - the framesize in centimeters cannot be the same as the framesize in inches! But I agree that a switch statement is more elegant than a series of if.....elses.

Last edited by Philip M; 02-15-2010 at 07:25 PM.. Reason: Typos
Philip M is offline   Reply With Quote
Old 02-15-2010, 07:22 PM   PM User | #5
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,856
Thanks: 9
Thanked 288 Times in 284 Posts
Dormilich is on a distinguished road
I’d calculate the value only once and calculate the inches from the centimeters just before output (last line, so-to-speak)
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 02-16-2010, 11:22 AM   PM User | #6
nico_icecold
New Coder

 
Join Date: Dec 2008
Posts: 23
Thanks: 6
Thanked 0 Times in 0 Posts
nico_icecold is an unknown quantity at this point
Thanks Guys - I have used the switch statement as it is a little more simple, but only issue I am having is calling the function on the the button press

Here is what I have

Code:
<script type="text/javascript">

function CalcStuff(act) {
  var a = parseFloat(BikeSizer.Insleg.value); 
  switch (act) {
   case '1' :	// Mountainbike
     b = Math.round(a * 0.572);
     c = Math.round(a * 0.226);
     break;
   case '2' :	// Trekking-, Reise- oder Cityrad
     b = Math.round(a * 5);
     c = Math.round(a * 10);
     break;
   case '3' :	// Rennrad
     b = Math.round(a * 10);
     c = Math.round(a * 20);
     break;
   default : alert('Invalid selection');  break;
  }
  document.BikeSizer.FramesizeCM.value = b; 
  document.BikeSizer.FramesizeInch.value = c;
}
</script>
The button is like this:
Code:
<button type="button" style="background:none; border:none;" onClick="CalcStuff(this.value)">
    <img src="images/berechnen_btn.gif" alt="berechnen" width="92" height="22">
</button>
Any ideas where I am going wrong?

Here is a live link to where I am:

http://www.nico-online.co.uk/calculator/calc1.html
nico_icecold is offline   Reply With Quote
Old 02-16-2010, 11:31 AM   PM User | #7
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,856
Thanks: 9
Thanked 288 Times in 284 Posts
Dormilich is on a distinguished road
Quote:
Originally Posted by nico_icecold View Post
Any ideas where I am going wrong?
yes.
Code:
<button type="button" style="background:none; border:none;" onClick="CalcStuff(this.value)">
in your script there is no need to pass anything. change act to document.BikeSizer.BikeType.value.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Users who have thanked Dormilich for this post:
nico_icecold (02-16-2010)
Old 02-16-2010, 11:32 AM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
<button type="button" style="background:none; border:none;" onClick="CalcStuff(this.value)">

this.value refers to the value of "this", in this case the button.

Code:
function CalcStuff() {
var act = document.Bikesizer.Biketype.options[document.myform.Biketype.selectedIndex].value;
Donnerwetter! Dormilich me to it has beaten!

Last edited by Philip M; 02-16-2010 at 11:36 AM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
nico_icecold (02-16-2010)
Old 02-16-2010, 11:46 AM   PM User | #9
nico_icecold
New Coder

 
Join Date: Dec 2008
Posts: 23
Thanks: 6
Thanked 0 Times in 0 Posts
nico_icecold is an unknown quantity at this point
Great .. Thanks guys All works nicely :
http://www.nico-online.co.uk/calculator/calc1.html
nico_icecold is offline   Reply With Quote
Old 02-16-2010, 12:06 PM   PM User | #10
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,856
Thanks: 9
Thanked 288 Times in 284 Posts
Dormilich is on a distinguished road
Quote:
Originally Posted by Philip M View Post
Code:
function CalcStuff() {
var act = document.Bikesizer.Biketype.options[document.myform.Biketype.selectedIndex].value;
just a question … the value of the select is really easy to get (select.value), but I oh so often see the access via selectedIndex (select.options[select.selectedIndex].value). then I ask myself, why so complicated for a non-multiple select …
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 02-16-2010, 12:27 PM   PM User | #11
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Dormilich View Post
just a question … the value of the select is really easy to get (select.value), but I oh so often see the access via selectedIndex (select.options[select.selectedIndex].value). then I ask myself, why so complicated for a non-multiple select …
This issue was raised here some time ago, and there was a feeling that select.value might not work in certain (unspecified) browsers, while selectedIndex (select.options[select.selectedIndex].value) was thought to be sure to work in all browsers. How valid that is I do not know.
Philip M is offline   Reply With Quote
Old 02-16-2010, 12:30 PM   PM User | #12
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,856
Thanks: 9
Thanked 288 Times in 284 Posts
Dormilich is on a distinguished road
that unspecified browser?
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 02-16-2010, 12:46 PM   PM User | #13
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Dormilich View Post
that unspecified browser?
No, not IE! Possibly Konquerer, dildo (?) or something.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Tags
calculator, conditional, form, menu, options

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 +1. The time now is 11:35 AM.


Advertisement
Log in to turn off these ads.