469,600 Members | 2,393 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,600 developers. It's quick & easy.

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
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. <!--
  3.  function Select()
  4.      {
  5.      if (document.getElementById('one').checked )
  6.      {document.getElementById('class').disabled=false
  7.      }
  8.   if else (document.getElementById('two').checked )
  9.   {document.getElementById('class').disabled=false}
  10.     if else
  11.      {document.getElementById('class').disabled=true}
  12.  }
  13. //-->
  14. </script>
  15. p> 2. Which our classes have you attended?<br/>
  16. <input type="checkbox" name="classes"  value="1" id="one"  onclick="Select(this);" /> Aerobics</br>
  17. <input type="checkbox" name="classes"  value="2"  id="two"  onclick="select(this);"  /> Boxing<br/>
  18. <input type="checkbox" name="classes" value="3"   id="three"  onclick="select(this);"  /> Circuit Class<br/>
  19. <input type="checkbox" name="classes"  value="4"   id="four"  onclick="select(this);"  /> Weight Training </p>
  21. <p> 3. Which of the above classes has been <b> most</b> beneficial for you?<br/>
  22. (You can only choose one from list)
  23. <select name="class" id="class" disabled="true" size="1"  style="color:#FFFFFF; background-color:#8A2BE2">
  24. <option value="Choose"> - -Please Choose- -  </option>
  25. <option value="AER">Aerobics</option>  
  26. <option value="Box">Boxing</option>
  27. <option value="Circuit">Circuit Class</option>
  28. <option value="Weight">Weight Training</option></select></p>
Apr 2 '09 #1
3 918
785 Expert 512MB
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.
Apr 2 '09 #2
8,652 Expert Mod 8TB
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
Expand|Select|Wrap|Line Numbers
  1.  function Select()
  2. {
  3.      if (this.checked)
  4.      {
  5.           document.getElementById('class').disabled = false;
  6.      }
  7.  }
Apr 2 '09 #3
thanks alot its working now
Apr 2 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

By using this site, you agree to our Privacy Policy and Terms of Use.