javascript problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lolodede
    New Member
    • Apr 2009
    • 63

    javascript problem

    hi i have trouble with disabled the select list when any of checkboxes is not checked and enable if one of them is checked this is my code
    Code:
    <script type="text/javascript">
    <!--
     function Select()
         {
         if (document.getElementById('one').checked )
         {document.getElementById('class').disabled=false
    	 }
      if else (document.getElementById('two').checked )
      {document.getElementById('class').disabled=false}
    	if else
         {document.getElementById('class').disabled=true}
     }
    //-->
    </script>
    p> 2. Which our classes have you attended?<br/>
    <input type="checkbox" name="classes"  value="1" id="one"  onclick="Select(this);" /> Aerobics</br>
    <input type="checkbox" name="classes"  value="2"  id="two"  onclick="select(this);"  /> Boxing<br/>
    <input type="checkbox" name="classes" value="3"   id="three"  onclick="select(this);"  /> Circuit Class<br/>
    <input type="checkbox" name="classes"  value="4"   id="four"  onclick="select(this);"  /> Weight Training </p>
    
    <p> 3. Which of the above classes has been <b> most</b> beneficial for you?<br/>
    (You can only choose one from list)
    <select name="class" id="class" disabled="true" size="1"  style="color:#FFFFFF; background-color:#8A2BE2">
    <option value="Choose"> - -Please Choose- -  </option>
    <option value="AER">Aerobics</option>  
    <option value="Box">Boxing</option>
    <option value="Circuit">Circuit Class</option>
    <option value="Weight">Weight Training</option></select></p>
    Last edited by Dormilich; Apr 2 '09, 07:37 AM. Reason: added [code] tags
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    Javascript is not java, you were in the wrong forum. And use code-tags around your code. And you are messing with lowercase-uppercase function names (Select() vs. select()). That's why it doesn't work. Javascript and Java are case sensitive. And you can't just revert "else if". If the syntax is wrong, then javascript doesn't work at all! (you can see a small error sign in the corner of your browser). This is incontrast to HTML, where you miss an opening bracket from your <p> tag and it still works, skipping the unreadable tag. Just enable your javascript debugger (e.g. firebug in firefox), it will tell you the lines that are wrong.

    General tip: just edit a few lines only first and get it working with. Then add more. Add line, test, add line, test, and so on.
    You can't just type a whole full-blown program without testing and then expect it to work immediately.

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      swap "if" and "else" on lines 8 and 10 to get a valid if-elseif-else construct.

      [edit] darn, missed by seconds ;)

      you can also use "this" inside the function to simplify matters
      Code:
       function Select()
      {
           if (this.checked)
           {
                document.getElementById('class').disabled = false;
           }
       }
      Last edited by Dormilich; Apr 2 '09, 07:48 AM. Reason: added "this"

      Comment

      • lolodede
        New Member
        • Apr 2009
        • 63

        #4
        thanks alot its working now

        Comment

        Working...