SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    blonde.... Sarah's Avatar
    Join Date
    Jul 2001
    Location
    Berkshire, UK
    Posts
    7,442
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    how do you check if a checkbox is selected in js

    I have a number of search options and institutions which are selected by clicking on the checkbox to the side. How can I write some js to check that at least one institution has been selected?

    I can do it using text boxes but can't seem to figure out how to check if a checkbox is empty..
    collection=="" doesn't seem to work !!

    Thanks

    sarah
    Regular user

  2. #2
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is an example of one way you could do it:
    <html>
    <head>
    <title>Form Text Fields Create and Validate</title>
    <SCRIPT LANGUAGE="JavaScript">
    // obj contains the form object.
    function Valflds(obj)
    {
    count=0
    len=obj.length // get the number of fields in the form
    for(a=0;a<len;a++)
    {
    if(obj[a].type=="checkbox" && obj[a].checked) // only look at form elements that are checkbox type
    {
    count++ // count the number of checked boxes
    }
    }
    if(count==0)
    {
    alert("You must check at least one text box! ")
    return false // don't submit the form if no boxes are checked
    }

    }
    </SCRIPT>
    </head>
    <body>
    <FORM NAME="myform" METHOD="post" ACTION="mailto:mailme@mail.com" onSubmit="return Valflds(this)">
    Check Me<INPUT TYPE="checkbox" NAME="ckbox"><BR>
    Me Too <INPUT TYPE="checkbox" NAME="ckbox"><BR>
    Try Me <INPUT TYPE="checkbox" NAME="ckbox"><BR>
    <INPUT TYPE="submit" VALUE="Submit"><INPUT TYPE="reset" VALUE="Clear Fields">
    </FORM>
    </body>
    </html>

  3. #3
    blonde.... Sarah's Avatar
    Join Date
    Jul 2001
    Location
    Berkshire, UK
    Posts
    7,442
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    thank you, one of these days I must master JS...

    Regular user


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
  •