Javascript - Disabled and Re-enable select form element

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cptuser
    New Member
    • Mar 2007
    • 30

    Javascript - Disabled and Re-enable select form element

    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.

    Code:
    function annconf() {
    
    var vfullearly = document.getElementById('fullearly').value;
    
    if (vfullearly = 1)
    	{
    		document.getElementById('fullnorm').disabled=true;
    			
    	}
    else if (vfullearly = 0)
    	{
    		document.getElementById('fullnorm').disabled=false;
    	}
    
    }
    The HTML form:


    Code:
    <form>
    
    Price early: 
      <label>
        <select name="Full, Early Bird" id="fullearly" onchange="annconf();">
    <option value="0">No</option>
    <option value="1">Yes</option>
    </select>
      </label>
    </p>
    <p> Price Normal: 
      <select name="Full, Normal" id="fullnorm">
        <option value="0">No</option>
        <option value="1">Yes</option>
      </select>
    <form>
  • omerbutt
    Contributor
    • Nov 2006
    • 638

    #2
    have a look@ this one mybe it will help
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Untitled</title>
    <script language="JavaScript" type="text/javascript">
    <!--
    function checkSel(selName,selIndex,val,val2){
    if(selName=="sel1"&&selIndex!=0){
    document.forms[0]['sel'+val].disabled=false;
    }
    if(selName=="sel1"&&selIndex==0){
    document.forms[0]['sel'+val].disabled=true;
    document.forms[0]['sel'+val2].disabled=true;
    document.forms[0]['sel'+val].selectedIndex=0;
    document.forms[0]['sel'+val2].selectedIndex=0;
    }
    if(selName=="sel2"&&selIndex!=0){
    document.forms[0]['sel'+val].disabled=false;
    }
    if(selName=="sel2"&&selIndex==0){
    document.forms[0]['sel'+val].disabled=true;
    document.forms[0]['sel'+val].selectedIndex=0;
    }
    }
    //-->
    </script>
    </head>
    <body>
    <form>
    <select name="sel1" onchange="checkSel(this.name,this.selectedIndex,2,3)">
    <option>select</option>
    <option>blah1_1</option>
    <option>blah1_2</option>
    <option>blah1_3</option>
    </select>
    <select name="sel2" disabled onchange="checkSel(this.name,this.selectedIndex,3)">
    <option>select</option>
    <option>blah2_1</option>
    <option>blah2_2</option>
    <option>blah2_3</option>
    </select>
    <select name="sel3" disabled>
    <option>select</option>
    <option>blah3_1</option>
    <option>blah3_2</option>
    <option>blah3_3</option>
    </select>
    </form>
    </body>
    </html>
    Last edited by acoder; May 2 '09, 10:08 AM. Reason: Removed quote

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      The problem in your code, cptuser, is that in this line, for example
      Code:
      if (vfullearly = 1)
      you're setting instead of comparing. Use double equals to compare:
      Code:
      if (vfullearly == 1)

      Comment

      • cptuser
        New Member
        • Mar 2007
        • 30

        #4
        Terrific thanks!
        "=" means set
        "==" means compare

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Yep, that's correct :)

          Comment

          Working...