October 18th, 2002, 05:25 PM
-
Why isn't this simple JavaScript code working?
Racking my brain here .... 
Code:
<script>function viewcom() {
if (document.commission.models.selectedIndex.value eq '6000P') {
document.commission.amount.value = '$1.00'
}
if (document.commission.models.selectedIndex.value eq '6000') {
document.commission.amount.value = '$0.90'
}
}</script>
<form name=commission><select name=models onChange="viewcom()"><option value="">- Select a model -</option>
<option value='6000P'>6000P</option>
<option value='6000'>6000</option>
<input name=amount></form>
Last edited by RandyL712; October 18th, 2002 at 05:48 PM.
October 18th, 2002, 06:54 PM
-
document.commission.models.selectedIndex returns an integer (the selected index of the models select element. You then try to access the value member of the returned integer, which doesn't exist.
You need to get the index, then use the models array with that index number to get the value.
Code:
document.commission.models[document.commission.models.selectedIndex].value
Also, you never had a closing select tag. Furthermore, eq is a Perl operator, and doesn't exist in javascript (which I'm assuming you're trying to use, since you never specified a language in your script element). It's also common practice to put scripts in your head element.
Try this:
Code:
<html>
<head>
<script language="Javascript">
function viewcom()
{
if (document.commission.models[document.commission.models.selectedIndex].value == '6000P') {
document.commission.amount.value = '$1.00'
}
if (document.commission.models[document.commission.models.selectedIndex].value == '6000') {
document.commission.amount.value = '$0.90'
}
}
</script>
</head>
<body>
<form name=commission><select name=models onChange="viewcom()"><option value="">- Select a model -</option>
<option value='6000P'>6000P</option>
<option value='6000'>6000</option>
</select>
<input name=amount></form>
</body>
</html>
October 18th, 2002, 06:59 PM
-
<script type="text/javascript" language="javascript">
function viewcom(sel) {
var v = sel[sel.selectedIndex].value;
switch (v) {
case '6000P' :
sel.form.amount.value = '$1.00';
break;
case '6000':
sel.form.amount.value = '$0.90';
break;
}
}
</script>
............
<form name="commission">
<select name="models" onchange="viewcom(this)">
<option value="">- Select a model -</option>
<option value="6000P">6000P</option>
<option value="6000">6000</option>
</select>
<input name="amount" type="text">
</form>
October 18th, 2002, 08:03 PM
-
Thanks - I'm having trouble going back and forth from JavaScript to Perl...
October 19th, 2002, 03:41 AM
-
It's funny because I had the same problem when I went from Perl to PHP.