Javascript checkbox checked
Results 1 to 7 of 7

Thread: Javascript checkbox checked

  1. #1
    Senior Member
    Join Date
    Apr 2005
    Posts
    1,250

    Javascript checkbox checked

    I have a form with about 25 checkbox's

    I need to ensure at least one is selected, how can I do this with so many checkbox's?

  2. #2
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,429
    Use either a commonality in the naming scheme (either of the 'name' or 'id' attributes) or use DOM methods on a common parent node to iterate over all of the checkboxes. Stop at the first one you find that is checked. If the loop terminates and you never found one, your condition wasn't met.

  3. #3
    Senior Member Derokorian's Avatar
    Join Date
    Apr 2011
    Location
    Denver
    Posts
    1,785
    jQuery approach:

    Code:
    var hasChecked = $('input[type=checkbox]:checked').length > 0;
    Last edited by Derokorian; 02-14-2013 at 02:59 PM.
    Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail
    I'd rather be a comma, then a full stop.
    User Authentication in PHP with MySQLi - Don't forget to mark threads resolved - MySQL(i) warning

  4. #4
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,429
    I suppose you could also do something similar without jQuery using XPath... although I do like the brevity of the jQuery syntax in this case.

  5. #5
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,889
    Quote Originally Posted by bradgrafelman View Post
    I suppose you could also do something similar without jQuery using XPath... although I do like the brevity of the jQuery syntax in this case.
    The DOM/XPath version is about three times longer...
    Code:
    var hasChecked = document.evaluate('.//input[@type="checkbox"][@checked]',
                     document, null, XPathResult.BOOLEAN_TYPE, null).booleanValue;
    If you're otherwise using jQuery there's no reason not to use it for this, but if you're not otherwise using jQuery it's a bit much.
    Last edited by Weedpacket; 02-14-2013 at 03:34 PM.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  6. #6
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,429
    Quote Originally Posted by Weedpacket View Post
    The DOM/XPath version is about three times longer...
    ... unless, of course, your website has no other use of jQuery, in which case I would say the DOM/XPath version (or even the simplistic approach I proposed above) is several orders of magnitudes smaller when compared with (the above jQuery statement + the jQuery library).

    @NZ_Kiwis: Moral of the story: There's more than one way to skin a mongoose. (Actually, there are three...)

  7. #7
    Senior Member Derokorian's Avatar
    Join Date
    Apr 2011
    Location
    Denver
    Posts
    1,785
    I agree, if this is the only thing you use jQuery on its not necessary to include the whole library for something like simple. However, personally I have lots of jQuery all throughout sites I've worked on, so I'm just used to doing it all in jQuery.
    Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail
    I'd rather be a comma, then a full stop.
    User Authentication in PHP with MySQLi - Don't forget to mark threads resolved - MySQL(i) warning

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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