Advanced Search

Results 1 to 4 of 4

Thread: Controlling Checkboxes

  1. #1
    Join Date
    Mar 2006
    Posts
    41
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Controlling Checkboxes

    When catcheck1 is unchecked, I want catcheck9 to uncheck. Why does this not work?
    <html>
    <head>
    <script type="text/javascript">
    // Checkbox event listeners.
    function checkboxs() {
    document.mapform.catcheck9.checked = false;
    }
    </script>
    </head>
    <body>
    <form action="" name="mapform" id="mapform">
    <input type="checkbox" id="catcheck1" name="catcheck9" onclick="checkboxs()" checked />
    <input type="checkbox" id="catcheck9" name="catcheck9" checked />
    </form>
    </body>
    </html>

  2. #2
    Join Date
    May 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    try delete the red items ...

    if you need the checkbox items checked by default.
    You can write a small script and add it to window.onload eventhandler to load automatically on page load.

    some recommendation:
    It is best to use same value on id and name attributes.
    Also, the checked should be best to rewrite as checked="checked" to be validated with (x)html.

    Code:
    <input type="checkbox" id="catcheck1" name="catcheck1" onclick="checkboxs()" checked />
    <input type="checkbox" id="catcheck9" name="catcheck9" checked />

  3. #3
    Join Date
    Jul 2008
    Posts
    128
    Thanks
    0
    Thanked 17 Times in 16 Posts

    Default

    Quote Originally Posted by JBottero View Post
    When catcheck1 is unchecked, I want catcheck9 to uncheck. Why does this not work?

    function checkboxs() {
    document.mapform.catcheck9.checked = false;
    }
    document.mapform.catcheck9 refers to an array of checkboxes all with the name 'catcheck9'.

    Your function doesn't check the state of the triggering checkbox.
    This is the simplest change to do what you want, but it's a poor solution:

    Code:
    function checkboxs() 
    {
     if( !document.mapform.catcheck9[0].checked )
      document.mapform.catcheck9[1].checked = false;
    }
    Are you going to want this to work with more elements?

  4. #4
    Join Date
    Jul 2008
    Posts
    128
    Thanks
    0
    Thanked 17 Times in 16 Posts

    Default

    Quote Originally Posted by Blueray2048 View Post

    if you need the checkbox items checked by default.
    You can write a small script and add it to window.onload eventhandler to load automatically on page load.
    Why would you do that? It makes the form script-dependent.
    some recommendation:
    It is best to use same value on id and name attributes.
    NOT in the case of form elements which are to be grouped by name.

Bookmarks

Posting Permissions

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