Results 1 to 5 of 5

Thread: Javascript unCheck function only working one way

  1. #1
    Join Date
    Jul 2005
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Javascript unCheck function only working one way

    Hey guys,
    I'm working on a simple javascript function for some checkboxes that I have in a form. If one is checked the other needs to be unchecked. Kind of like a radio button but I need these to be separate inputs. Using the code below, two will uncheck one but one will not uncheck two. What am I doing wrong?

    Code:
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script>
    function unCheck(Form, Name){
    	document.forms[Form].elements[Name].checked = false;
    }
    </script>
    </head>
    
    <body>
    <form name="something">
    <input type="checkbox" name="something1" value="1" onclick"javascript:unCheck(this.form.name, 'something2');" /> Something 1
    <br />
    <input type="checkbox" name="something2" value="1" onclick="javascript:unCheck(this.form.name, 'something1');" /> Something 2
    </form>
    </body>
    </html>

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    There are a number of questionable practices here. But the only real problem is that you left out the equals sign in the first onclick event:

    Code:
    <input type="checkbox" name="something1" value="1" onclick"javascript:unChe . . .
    should be:

    Code:
    <input type="checkbox" name="something1" value="1" onclick="javascript:unChe . . .
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jul 2005
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    HOLY COW!

    My membership in the forum should be revoked for that!

    How would you recommend doing this?

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    XHTML shouldn't be used unless your document really is XHTML/xml which yours was not, and doesn't need to be. Very few need to be. XHTML is just a fad in most cases and does nothing for a page that HTML 4.01 strict does not, and can sometimes cause problems. The XHTML xml standard will most likely be completely revamped before it has any genuine wide application.

    That said, converting to valid HTML 4.01 strict, and simplifying the javascript:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Untitled Document</title>
    <script type="text/javascript">
    function unCheck(el, n){
    el.form.elements[n].checked = false;
    }
    </script>
    </head>
    
    <body>
    <form name="something" action="#">
    <div>
    <input type="checkbox" name="something1" value="1" onclick="unCheck(this, 'something2');"> Something 1<br>
    <input type="checkbox" name="something2" value="1" onclick="unCheck(this, 'something1');"> Something 2
    </div>
    </form>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jul 2005
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thanks, it works perfect...

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
  •