...

View Full Version : How to Refer to a Selection Box



slvermoondragon
03-17-2009, 12:44 AM
Hello, I have to add a selection box in my assignment, but I don't know how to refer to it in the javascript.

Here is the javascript code I am trying to refer to the selection box with that doesn't work:


if (document.model.owlconditions.value = normal)
{
window.alert("this is working-normal");
owl = num4 * 1.25;
}
if (document.model.owlconditions.value = trapping)
{
window.alert("this is working-trapping");
owl = num4 - num4*0.2;
}
if (document.model.owlconditions.value = rabies)
{
window.alert("this is working-rabies");
owl = num4 - num4*0.4;
}

And here is the html code for the actual selection box:



<select name="owlconditions" style="height: 16px">
<option selected value="normal">Normal</option>
<option value="trapping">Trapping</option>
<option value="rabies">Rabies</option>
</select>

Thanks for your help

jmrker
03-17-2009, 12:58 AM
For all your "if (condition)" statments, use '==' and not '=' to create the condition.

slvermoondragon
03-17-2009, 01:07 AM
I tried that, but it is still not working.

jmrker
03-17-2009, 01:19 AM
More questions ...

1. Have you checked the error console in FF browser for errors?
2. Can you post the rest of the JS and HTML code or provide a link if live?
3. What browser are you using?

slvermoondragon
03-17-2009, 01:50 AM
1. Have you checked the error console in FF browser for errors?
Internet Explorer says "Line:63, Char:9, Error: 'trapping' is undefined, Code:0"
FireFox says "normal is not defined, Line: 63 and trapping is not defined, Line 63" there are a bunch of these errors
2. Can you post the rest of the JS and HTML code or provide a link if live?
here is a live link (http://1200webs.cis.uoguelph.ca/~kvoute/assignment5second.html)


<html>
<head>

<title>Owls vs Mice Population Model</title>

<script language="JavaScript">
function population()
{
var num1, num2, num3, num4, num5, owl, micepop, owlfood, deathpercent, birthpercent, stars, newyear, deathvalue, birthvalue, totalmice;

num1 = parseFloat(document.model.mice.value);
num2 = parseFloat(document.model.death.value);
num3 = parseFloat(document.model.birth.value);
num4 = parseFloat(document.model.owls.value);
num5 = parseInt(document.model.year.value);

newyear = num5 + 1;

if (document.model.conditions[0].checked==true) /* normal conditions */
{
deathvalue = 4;
birthvalue = 9;
deathpercent = deathvalue/100;
birthpercent = birthvalue/100;
micepop = num1 - num1*deathpercent + num1*birthpercent;
}
if (document.model.conditions[1].checked==true) /* cold conditions */
{
document.model.conditions[0].checked = true;
deathvalue = num2 + 3;
birthvalue = num3;
deathpercent = deathvalue/100;
birthpercent = birthvalue/100;

micepop = num1 - num1*deathpercent + num1*birthpercent;
}
if (document.model.conditions[2].checked==true) /* fire conditions */
{
document.model.conditions[0].checked = true;
deathvalue = num2 * 2;
birthvalue = num3;
deathpercent = deathvalue/100;
birthpercent = birthvalue/100;

micepop = num1 - num1*deathpercent + num1*birthpercent;
}
if (document.model.conditions[3].checked==true) /* exellent conditions */
{
document.model.conditions[0].checked = true;
deathvalue = num2 - 1;
birthvalue = num3 * 2;
deathpercent = deathvalue/100;
birthpercent = birthvalue/100;

micepop = num1 - num1*deathpercent + num1*birthpercent;
}
if (micepop<5000)
{
window.alert("Too many Owls - Starvation Hits");
owl = num4 - num4*0.6;
}
if (document.model.owlconditions.value == trapping)
{
window.alert("this is working-normal");
owl = num4 * 1.25;
}
if (document.model.owlconditions.value == normal)
{
window.alert("this is working-trapping");
owl = num4 - num4*0.2;
}
if (document.model.owlconditions.value == rabies)
{
window.alert("this is working-rabies");
owl = num4 - num4*0.4;
}
if (owl<2)
{
owl=2
}

owlfood = owl * (3 * 52);
totalmice = micepop - owlfood

document.model.mice.value = totalmice;
document.model.death.value = deathvalue;
document.model.birth.value = birthvalue;
document.model.owls.value = owl;
document.model.year.value = newyear;

/* graph portion */

if (document.model.graphyes.checked)
{
stars = parseInt(document.model.mice.value)/500;

document.model.graph.value = document.model.graph.value + newyear + " "

for ( var count = 1; count <= stars; count = count +1)
{
document.model.graph.value = document.model.graph.value + "*";
}

document.model.graph.value = document.model.graph.value + "\n";
}
}
</script>

</head>
<body bgcolor="#CEF5FF">

<p style="font-size: x-large"><u><b>Owls vs Mice Population Model</b></u></p>
<form name="model">
<table border="3" style="background-color:#AAAAFF">
<tr>
<td>No. of Mice</td>
<td style="text-align: center"><input name="mice" type="text" value="10000" style="width: 70px"></td>
</tr>
<tr>
<td>Death Rate</td>
<td style="text-align: center"><input name="death" type="text" value="4" style="width: 70px"></td>
</tr>
<tr>
<td>Birth Rate</td>
<td style="text-align: center"><input name="birth" type="text" value="9" style="width: 70px"></td>
</tr>
<tr>
<td>No. of Owls</td>
<td style="text-align: center"><input name="owls" type="text" value="3" style="width: 70px"></td>
</tr>
<tr>
<td>Mice Conditions</td>
<td>Normal<input name="conditions" checked type="radio">
Cold<input name="conditions" type="radio">
Fires<input name="conditions" type="radio">
Excellent<input name="conditions" type="radio"></td>
</tr>
<tr>
<td>Graph the Owls</td>
<td><input name="graphyes" checked type="checkbox"></td>
</tr>
<tr>
<td><input type="button" value="One year passes" onclick = "population()"></td>
<td><input name="year" type="text" value="0" style="width: 70px"></td>
</tr>
</table>

Owl Conditions
<select name="owlconditions" style="height: 16px">
<option selected value="normal">Normal</option>
<option value="trapping">Trapping</option>
<option value="rabies">Rabies</option>
</select>
<p style="font-size:large"><b>The Number of Mice</b></p>
<textarea name="graph" rows="10" cols="35"></textarea><br>
<input type="button" value="Clear the Graph" onclick="document.model.graph.value = ''">
<input type="button" value="Reset Form" onclick="reset()">
</form>

</body>
</html>

3. What browser are you using?
Mozilla Firefox 3.0.7 and I have tried in IE 7.0

jmrker
03-17-2009, 02:02 AM
Since the are string values, it should be


if (document.model.owlconditions.value == 'trapping')
{
window.alert("this is working-normal");
owl = num4 * 1.25;
}
if (document.model.owlconditions.value == 'normal')
{
window.alert("this is working-trapping");
owl = num4 - num4*0.2;
}
if (document.model.owlconditions.value == 'rabies')

Fix any other errors found...:thumbsup:

slvermoondragon
03-17-2009, 02:19 AM
Thank you so much.

jmrker
03-17-2009, 03:24 AM
You're most welcome.
Glad I was able to help.
Good Luck!
:)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum