SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    London
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to check at least one checkbox is checked

    Hi,
    I have a page where I dynamically generate several checboxes. The number varies.
    I need a simple script that will count the checkboxes and alert if none are checked. I have made a script that checks a predetermined number of checkboxes not for when there is an unknown number of checkboxes. Can anyone help here?
    thanks

  2. #2
    SitePoint Evangelist Mr. Brownstone's Avatar
    Join Date
    May 2002
    Location
    Relative
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Of course, that's just my opinion. I could be wrong.

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    London
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mr. Brownstone
    Hi,thanks, but theres a problem with this, it requires that all names are supplied to the function as parameters, which I dont actually have, cos I am dynamically creating the checkboxes on the fly. So I do not know in advance how many checkboxes there will be on a page,
    any soution that would incorporate this?
    thanks

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,276
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    okay, here ya go --

    define this javascript function:
    Code:
    <script type="text/javascript">
    function checkboxen(theform) {
    var howmany=0
    var checked=0
    var str=''
      for (i = 0; i < theform.elements.length; i++)  {
        if (theform.elements[i].type == 'checkbox')  {
           howmany=howmany+1;
           if(theform.elements[i].checked == true) {
             checked=checked+1
             str=str+ theform.elements[i].name+' '
           }  
        }
      }  
    alert('boxes='+howmany+'\nchecked='+checked+'\n'+str)
    }
    </script>
    and then invoke it like this:
    Code:
    <form name="f"
     onsubmit="checkboxen(this); return false;" >
    <input type="checkbox" name="box1" />Box1<br />
    <input type="text" name="box2" value="box 2" />
    <input type="hidden" name="box2a" value="2a" /><br />
    <input type="checkbox" name="foo" />Foo<br />
    <input type="checkbox" name="box3" />Box3<br />
    <input type="checkbox" name="bar" />Bar<br />
    <input type="submit" value="How many checkboxes?">
    </form>
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Just optimizing Rudy's function a bit...
    Code:
    <script type="text/javascript">
    function checkboxen(theform) {
    var howmany=0
    var checked=0
    var str=''
      for (i = 0; j = theform.elements.length; i < j; i++)  {
        if (theform.elements[i].type == 'checkbox')  {
           howmany++;
           if(theform.elements[i].checked) {
             checked++;
             str += theform.elements[i].name+' ';
           }  
        }
      }  
    alert('boxes='+howmany+'\nchecked='+checked+'\n'+str)
    }
    </script>

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,276
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    thanks vinnie

    my javascript's not all that great -- i mean, i can write it okay (after all, i did okay on this one, eh?) -- but the finer points of optimization, like setting j, well, that's still a bit advanced for me

    so thanks for the pointer

    rudy

  7. #7
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by r937
    thanks vinnie

    my javascript's not all that great -- i mean, i can write it okay (after all, i did okay on this one, eh?) -- but the finer points of optimization, like setting j, well, that's still a bit advanced for me

    so thanks for the pointer

    rudy
    Hey you did pretty well, and I'm sure Beetle or MikeFoster will come in and school me as far as optimization goes too .


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
  •