Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How to Refer to a Selection Box

    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:
    Code:
    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:

    Code:
    <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

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,145
    Thanks
    38
    Thanked 505 Times in 499 Posts
    For all your "if (condition)" statments, use '==' and not '=' to create the condition.

  • #3
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I tried that, but it is still not working.

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,145
    Thanks
    38
    Thanked 505 Times in 499 Posts

    Question Need more information ...

    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?

  • #5
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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
    Code:
    <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

  • #6
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,145
    Thanks
    38
    Thanked 505 Times in 499 Posts

    Arrow

    Since the are string values, it should be
    Code:
            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...

  • Users who have thanked jmrker for this post:

    slvermoondragon (03-17-2009)

  • #7
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you so much.

  • #8
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,145
    Thanks
    38
    Thanked 505 Times in 499 Posts
    You're most welcome.
    Glad I was able to help.
    Good Luck!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •