Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with IF/Else syntax

Status
Not open for further replies.

rry2k

Programmer
Jun 28, 2001
678
US
I'm creating a menu and have started to add if/else logic.
I'm getting errors which I think are being generated by the braces being wrong. Please help. Thanks..Russ

<html>
<head>
<title>menu</title>
<script language=&quot;javascript&quot;>
function Navigate() {
prod = document.menuform.woodtype.selectedIndex;
val = document.menuform.woodtype.options[prod].value;
icolor = document.menuform.woodcolor.selectedIndex;
ival = document.menuform.woodcolor.options[icolor].value;
if (val = &quot;cherry&quot;) {
document.write(&quot;The type selected is &quot; + val + &quot;<br>&quot;);
else
document.write(&quot;The color selected is &quot; + ival);
}
}
</script>
</head>
<body>
<form name=&quot;menuform&quot;>
<b>type of wood</b>
<select name =&quot;woodtype&quot;>
<option value=&quot;mahogony&quot;>mohogany
<option value=&quot;maple&quot;>maple
<option value=&quot;pine&quot;>pine
<option value=&quot;cherry&quot;>cherry
</select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
<input type=&quot;button&quot; name=&quot;go&quot; value = Go to Function&quot;
onClick=&quot;Navigate();&quot;>
<b>color of wood</b>
<select name =&quot;woodcolor&quot;>
<option value=&quot;Red&quot;>red
<option value=&quot;brown&quot;>brown
<option value=&quot;white&quot;>white
<option value=&quot;beige&quot;>beige
</select>
</body>

</form>
</html>
 
Try:

Code:
<script language=&quot;javascript&quot;>
function Navigate() {
prod = document.menuform.woodtype.selectedIndex;
val = document.menuform.woodtype.options[prod].value;
icolor = document.menuform.woodcolor.selectedIndex;
ival = document.menuform.woodcolor.options[icolor].value;
if (val = &quot;cherry&quot;) {
document.write(&quot;The type selected is &quot; + val + &quot;<br>&quot;); 
}
else {
document.write(&quot;The color selected is &quot; + ival);
}
}
</script>
 
Code:
<html>
<head>
<title>menu</title>
<script language=&quot;javascript&quot;>
function Navigate() {
 prod = document.menuform.woodtype.selectedIndex;
 val = document.menuform.woodtype.options[prod].value;
 icolor = document.menuform.woodcolor.selectedIndex;
 ival = document.menuform.woodcolor.options[icolor].value;
 if (val == &quot;cherry&quot;) {
 document.write(&quot;The type selected is &quot; + val + &quot;<br>&quot;);
 }
 else {
 document.write(&quot;The color selected is &quot; + ival);
 }
}
</script>
</head>
<body>
<form name=&quot;menuform&quot;>
<b>type of wood</b>
    <select name =&quot;woodtype&quot;>
    <option value=&quot;mahogony&quot;>mohogany
    <option value=&quot;maple&quot;>maple
    <option value=&quot;pine&quot;>pine
    <option value=&quot;cherry&quot;>cherry
    </select>      
    <input type=&quot;button&quot; name=&quot;go&quot; value = Go to Function&quot;
    onClick=&quot;Navigate();&quot;>
<b>color of wood</b>
<select name =&quot;woodcolor&quot;>
<option value=&quot;Red&quot;>red
<option value=&quot;brown&quot;>brown
<option value=&quot;white&quot;>white
<option value=&quot;beige&quot;>beige
</select>
</body>
</form>
</html>
here's your fixed code and a couple of tips:
1. you have only enclosed an entire if/else statements into brackets, but in js you actually need to enclose each if and else statement into its own brackets like:
if (condition) {
do if condition is true
}
else {
do else
}

2. please note that I have changed your condition from (val = &quot;cherry&quot;) to (val == &quot;cherry&quot;)
when you only write one &quot;=&quot; it means assigning a value.
i.e. when you say val = &quot;cherry&quot; you assing a value &quot;cherry&quot; to the variable named val
on the other hand, when you use double &quot;==&quot; it means comparison. I was told a neat thing once. think of it as &quot;exactly equals&quot; and then you will always remember to put two equal signs.
when you say if (val == &quot;cherry&quot;) you are saying does a variable of val exactly equal &quot;cherry&quot;?

HTH --------------------------------------------------
Goals are dreams with deadlines
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top