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!
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."
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
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>
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
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.