SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Designer
    Join Date
    Jun 2006
    Location
    Manila
    Posts
    590
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Little Javascript checkbox question

    Somewhat of a noob/very rusty JS user here, so pardon the question.

    I need to create two checkboxes, but they can never be unchecked. Basically the default is: that both boxes are checked when the page loads. However, a user can uncheck one or the other, but if they try to uncheck both, it reverts to the default (both are checked).

    Any good samaritans here who can shed some light?

  2. #2
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <html>
    <
    head>
    <
    title>Untitled Document</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <
    script type="text/JavaScript">
    function 
    checkXBox(){
        var 
    checkBox1document.getElementById('checkBox1');
        var 
    checkBox2document.getElementById('checkBox2');
        if(!
    checkBox1.checked && !checkBox2.checked){
            
    checkBox1.checkedtrue;
            
    checkBox2.checkedtrue;
        }
    }
    </script>
    </head>
    <body>
    <form action="" method="post" id="myid">
    <input name="one" type="checkbox" value="1" checked="checked" id="checkBox1" onClick="checkXBox()">
    <input name="two" type="checkbox" value="2" checked="checked" id="checkBox2" onClick="checkXBox()">
    </form>
    </body>
    </html> 
    Bye.

  3. #3
    Designer
    Join Date
    Jun 2006
    Location
    Manila
    Posts
    590
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers mate.

    (No "Hi" before the "bye?")

  4. #4
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by XLCowBoy View Post
    (No "Hi" before the "bye?")
    sorry

  5. #5
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could use a radio input except that you can't check both with a radio.
    mmj

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    You could though provide a third radio button called "Both", which depending on context may be more understandable. With checkboxes it may be difficult for people to intuitively understand why at least one of them must be ticked.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    Designer
    Join Date
    Jun 2006
    Location
    Manila
    Posts
    590
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    You could though provide a third radio button called "Both", which depending on context may be more understandable. With checkboxes it may be difficult for people to intuitively understand why at least one of them must be ticked.
    Actually, I had restyled them to look like icons. And when one is unchecked, a cross "X" shows up over it. It's the search system for a vehicle dealer that sells bikes and cars - so it's either (a) search both vehicles, or (b) "cross-out" the one that you don't wish to include in the search. Going by that, it would be impossible for somebody to search for "neither", which is why the script undoes that attempt.


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
  •