By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,528 Members | 1,291 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,528 IT Pros & Developers. It's quick & easy.

Javascript - Disabled and Re-enable select form element

P: 30
Hi,

I have an online form which has a select element, which I;m using very basic JS to enable and disable a select element based on the selection of another select element.

For some reason, the JS disables the select element correctly, but it does not re-enable it. Could someone please help with the right code.

here is the Javascript code.

Expand|Select|Wrap|Line Numbers
  1. function annconf() {
  2.  
  3. var vfullearly = document.getElementById('fullearly').value;
  4.  
  5. if (vfullearly = 1)
  6.     {
  7.         document.getElementById('fullnorm').disabled=true;
  8.  
  9.     }
  10. else if (vfullearly = 0)
  11.     {
  12.         document.getElementById('fullnorm').disabled=false;
  13.     }
  14.  
  15. }
The HTML form:


Expand|Select|Wrap|Line Numbers
  1. <form>
  2.  
  3. Price early: 
  4.   <label>
  5.     <select name="Full, Early Bird" id="fullearly" onchange="annconf();">
  6. <option value="0">No</option>
  7. <option value="1">Yes</option>
  8. </select>
  9.   </label>
  10. </p>
  11. <p> Price Normal: 
  12.   <select name="Full, Normal" id="fullnorm">
  13.     <option value="0">No</option>
  14.     <option value="1">Yes</option>
  15.   </select>
  16. <form>
May 2 '09 #1
Share this Question
Share on Google+
4 Replies


omerbutt
100+
P: 638
have a look@ this one mybe it will help
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>Untitled</title>
  5. <script language="JavaScript" type="text/javascript">
  6. <!--
  7. function checkSel(selName,selIndex,val,val2){
  8. if(selName=="sel1"&&selIndex!=0){
  9. document.forms[0]['sel'+val].disabled=false;
  10. }
  11. if(selName=="sel1"&&selIndex==0){
  12. document.forms[0]['sel'+val].disabled=true;
  13. document.forms[0]['sel'+val2].disabled=true;
  14. document.forms[0]['sel'+val].selectedIndex=0;
  15. document.forms[0]['sel'+val2].selectedIndex=0;
  16. }
  17. if(selName=="sel2"&&selIndex!=0){
  18. document.forms[0]['sel'+val].disabled=false;
  19. }
  20. if(selName=="sel2"&&selIndex==0){
  21. document.forms[0]['sel'+val].disabled=true;
  22. document.forms[0]['sel'+val].selectedIndex=0;
  23. }
  24. }
  25. //-->
  26. </script>
  27. </head>
  28. <body>
  29. <form>
  30. <select name="sel1" onchange="checkSel(this.name,this.selectedIndex,2,3)">
  31. <option>select</option>
  32. <option>blah1_1</option>
  33. <option>blah1_2</option>
  34. <option>blah1_3</option>
  35. </select>
  36. <select name="sel2" disabled onchange="checkSel(this.name,this.selectedIndex,3)">
  37. <option>select</option>
  38. <option>blah2_1</option>
  39. <option>blah2_2</option>
  40. <option>blah2_3</option>
  41. </select>
  42. <select name="sel3" disabled>
  43. <option>select</option>
  44. <option>blah3_1</option>
  45. <option>blah3_2</option>
  46. <option>blah3_3</option>
  47. </select>
  48. </form>
  49. </body>
  50. </html> 
May 2 '09 #2

acoder
Expert Mod 15k+
P: 16,027
The problem in your code, cptuser, is that in this line, for example
Expand|Select|Wrap|Line Numbers
  1. if (vfullearly = 1)
you're setting instead of comparing. Use double equals to compare:
Expand|Select|Wrap|Line Numbers
  1. if (vfullearly == 1)
May 2 '09 #3

P: 30
Terrific thanks!
"=" means set
"==" means compare
May 2 '09 #4

acoder
Expert Mod 15k+
P: 16,027
Yep, that's correct :)
May 2 '09 #5

Post your reply

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