SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    About Checked Checkboxes

    Hello. As a javascript newbie , I wish to place all to be checked checkboxes of a form in an array for referencing. How can I achieve this ?

    Thanks in advance.

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Give them all the same NAME attribute:
    Code:
    <form id="theform">
    <input type="checkbox" name="mycheckbox" value="1" />
    <input type="checkbox" name="mycheckbox" value="2" />
    <input type="checkbox" name="mycheckbox" value="3" />
    <input type="checkbox" name="mycheckbox" value="4" />
    </form>
    Then in your javascript code:
    Code:
    var boxes = document.getElementById("theform").elements["mycheckbox"];
    Note that it's a collection of checkboxes rather than an array, but should do the trick.

  3. #3
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply @jimfraser. I especially want to refer to the user checked checkboxes. Say, user checked some checkboxes on the form but not the all. How can I retrieve back which checkboxes checked?

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    var boxes = document.getElementById("theform").elements["mycheckbox"];
    for (var i=0; i < boxes.length; i++) { // loop through our checkboxes
      // boxes[i] refers to the current checkbox - examine the "checked" property
      alert("Box " + (i+1) + " is " + (boxes[i].checked ? "" : "not ") + "checked");
    }


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
  •