SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Apr 2004
    Location
    Manchester
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Quick Checkbox question about ELEMENTS

    Hi all,

    I'm trying to find out how to get an integer of how many checkboxes (ELEMENTS?) are ticked in a field?

    I want to limit the number of checkboxes that can be checked to 5, and can't find anything in forum (it telle me how to return the value, not the length?)

    Can anyone please advise?

    Many thanks!

    Gonzo

  2. #2
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    scan checkboxes
    XChecked

    for (i=0;i<document.form.checkboxes.lenght){
    XChecked=(document.form.checkboxes[i].checked)?XChecked+1:XChecked;
    }
    alert('There are' + XChecked +' Boxes CHecked')

  3. #3
    SitePoint Member
    Join Date
    Apr 2004
    Location
    Manchester
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks Frog

    do I put this in a function/event or something? I'm getting error message telling me that there is a ; missing......

    not sure what the SCAN keyword does.....

    Ta!

    Gonzo

  4. #4
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <script type='text/javascript'>
    function ScanCheckboxes(){
    var XChecked;
    for (i=0;i<document.form.checkboxes.lenght){
    XChecked=(document.form.checkboxes[i].checked)?XChecked+1:XChecked;
    }
    alert('There are' + XChecked +' Boxes CHecked')
    }
    in the head of page

    in the body:
    Code:
    <input type='button' onclick="ScanCheckboxes()" value="how many?" />

  5. #5
    SitePoint Member
    Join Date
    Apr 2004
    Location
    Manchester
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks again

    Cheers again Mr Frog.

    I put the code in (I am embedding it in Lotus Notes), but it crashes the Browser - I suspect that this is a shortfall of Notes rather than your code so I shall endeavour to find another way. It's a pain!

    (I replaced the checkboxes name with "QuestionEleven", which is my notes field name - viewing the source code of page says this is so

    ------------------------

    function ScanCheckboxes(){
    var XChecked;
    for (i=0;i<document.form.QuestionEleven.length){
    XChecked=(document.form.QuestionEleven[i].checked)?XChecked+1:XChecked;
    }
    alert('There are' + XChecked +' Boxes CHecked')
    }

    -------------------------

    Many thanks again for your efforts!

    Gonzo

  6. #6
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should also replace form by name of your form (if there is one)

    if you have no form you will have to use another syntax such as
    Code:
    document.getElementsByName('QuestionEleven')


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
  •