Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Help needed with checkbox/dropdown code.

    Hello everyone. I have been trying 2 days straight to figure out how to make this script work. Basically I am trying to have a hidden checkbox with a drop down menu. I want the user to be able to choose 0-9 where 0 the check box will not be checked and 1-9 if picked from the dropdown menu it will automatically check the hidden box. Here is the closest string of code I could find out there to help me figure it out but I am having no luck. The closest I got was....well I never really figured out how to make it work. Can someone please help me? Thanks.

    <script type="text/javascript">
    function checkUncheck (selected) {
    var checkBox = document.getElementById("box");

    if ((selected == 2) || (selected == 3)) {
    checkBox.checked = "checked";
    }
    if ((selected == 0) || (selected == 1)) {
    checkBox.checked = "false";
    }
    }
    </script>
    <form id="frm">
    <table>
    <tr>
    <td><input type="checkbox" id="box" checked="false"/></td>

    <td>
    <select id="sel0" onchange="checkUncheck(this.selectedIndex)">
    <option value="zero">zero</option>
    <option value="one">one</option>

    <option value="two">two</option>
    <option value="three">three</option>
    </select>
    </td>
    </tr>

    </table>
    </form>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,570
    Thanks
    78
    Thanked 4,385 Times in 4,350 Posts
    Code:
    <form ...>
    ...
    <select name="whatever" onchange="this.form.theBox.checked = ( this.value != "0" );">
    <option value="0">zero</option>
    <option value="8">apples</option>
    <option value="3">bananas</option>
    ...
    </select>
    <input type="checkbox" name="theBox" style="visibility: hidden;">
    ...
    </form>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,570
    Thanks
    78
    Thanked 4,385 Times in 4,350 Posts
    Why do you give id's to form fields? If you will submit the <form> to another page (e.g., PHP or ASP or JSP or whatever) fields without names are *NEVER* submitted. The id's are useless. (Excepting that if you want to use <label for="xxx"> then the "xxx" must be an id, not a name.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,570
    Thanks
    78
    Thanked 4,385 Times in 4,350 Posts
    If you don't want the value= of the <option>s to be numbers, then you could indeed use selectedIndex, thus:
    Code:
    <form ...>
    ...
    <select name="whatever" onchange="this.form.theBox.checked = ( this.selectedIndex != 0 );">
    <option value="frammis">zero</option>
    <option value="imacs">apples</option>
    <option value="yellow">bananas</option>
    ...
    </select>
    <input type="checkbox" name="theBox" style="visibility: hidden;">
    ...
    </form>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,570
    Thanks
    78
    Thanked 4,385 Times in 4,350 Posts
    Or, come to think of it, you could even do this:
    Code:
    <form ...>
    ...
    <select name="whatever" onchange="this.form.theBox.checked = ( this.value != 'frammis' );">
    <option value="frammis">zero</option>
    <option value="imacs">apples</option>
    <option value="yellow">bananas</option>
    ...
    </select>
    <input type="checkbox" name="theBox" style="visibility: hidden;">
    ...
    </form>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •