Check & Disable a check box based on a dropdown menu options

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

    Check & Disable a check box based on a dropdown menu options

    Hi,

    I've put together this javascript taken from various sources, but i think it is wrong or missing something. If particular options are selected within a drop down menu, then the check botton will auto check and disable (grey out) at the same time. If seems like if will work fine but int he database it will wont show as selected, but if I manually check it, it will record fine in the DB so the form works, but not the javascript. Here is the code. i need help - i have no idea what is wrong.

    <script language="JavaS cript"> function updateCheckBox () {
    selectedItem = form1.scrp_arw. options [form1.scrp_arw. selectedIndex].value;
    if (selectedItem != '') {
    form1.scrp_home _cz.checked=tru e; form1.scrp_home _cz.disabled=tr ue; }
    else {
    form1.scrp_home _cz.disabled=fa lse form1.scrp_home _cv.checked=fal se;
    }
    }
    updateCheckBox ();
    </script>
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Welcome to TSDN.

    This may be a typo, but if the javascript is on one line, you should have a semi-colon. After the 'disabled=false ', you have a semi-colon missing.

    Comment

    • cptuser
      New Member
      • Mar 2007
      • 30

      #3
      I put the semi colon in and it did not make any different. I actually had it in the original code but when I pasted in my post above I forgot to add it.

      Do you have any other suggestions? Would it have somoething to do with the fact that there are two things happening to the same field at the same time and it can't handle it properly, would there be a better way to write the javascript, for example combine the action (check & disable) at the same time rather than seperate them?

      Comment

      • chunk1978
        New Member
        • Jan 2007
        • 224

        #4
        Originally posted by cptuser
        Would it have somoething to do with the fact that there are two things happening to the same field at the same time and it can't handle it properly...
        nope... i'm pretty sure this is not the reason... try this:

        Code:
        <script language="text/JavaScript">
        
        function updateCheckBox ()
             {
             var selectedItem = form1.scrp_arw.options[form1.scrp_arw.selectedIndex].value;
             if (selectedItem!="")
             {form1.scrp_home_cz.checked=true; form1.scrp_home_cz.disabled=true;}
             else 
             {form1.scrp_home_cz.disabled=false; form1.scrp_home_cv.checked=false;}
        }
        </script>
        and i'm pretty sure you also need to define in your HTML code weather your checkbox is initially Checked/Unchecked and Disabled/Enabled for the javascript to use true and false...

        Comment

        • cptuser
          New Member
          • Mar 2007
          • 30

          #5
          Originally posted by chunk1978
          nope... i'm pretty sure this is not the reason... try this:
          It's not working for me. I would like to try it another way. If the checkbox is checked, then the dropdown menu is enabled. If the checkbox is NOT checked then the dropdown menu is Disabled. I don't know what the whole javascript would be. Hope you can help me.

          Comment

          • cptuser
            New Member
            • Mar 2007
            • 30

            #6
            Checkbox enables/disables drop down list

            Anyone can share javascript code for this, please.

            If a checkbox is checked (default is unchecked), then a drop down list below it is enabled (that is not greyed out, the default is disabled – greyed out). If the user then unchecks the checkbox, then 2 things happen to the drop down menu, it is disabled (greyed out) and it’s value changes to an option with a value of nothing β€œβ€.

            Hope someone can assist. I have not idea.

            Comment

            • chunk1978
              New Member
              • Jan 2007
              • 224

              #7
              Code:
              <html>
              <head>
              
              <script type="text/javascript">
              function EnableSelectMenu()
              	{
              	if (document.getElementById('Checkbox').checked)
              	{document.getElementById('SelectMenu').disabled=false}
              	else
              	{document.getElementById('SelectMenu').disabled=true}
              }
              </script>
              
              </head>
              
              <body>
              <form action="" method="get">
                <p>
                  <input type="checkbox" name="Checkbox" id="Checkbox" value="checkbox" onClick="EnableSelectMenu();">
              Check To Enable Select Menu </p>
                <p>
                  <select name="SelectMenu" id="SelectMenu" disabled="disabled">
                    <option value="SelectionA">Selection A</option>
                    <option value="SelectionB">Selection B</option>
                    <option value="SelectionC">Selection C</option>
                  </select></p>
                </form>
              </body>
              </html>

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                I have merged the threads because they were on the same topic.

                Did you solve your problem?

                Comment

                • cptuser
                  New Member
                  • Mar 2007
                  • 30

                  #9
                  Yes it did thank you.

                  Comment

                  Working...