SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: checkBox

  1. #1
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    checkBox

    hello

    can anyone help me in how to know checkbox is checked when that check box is in if condition .
    if if condition is satisfied only i have checkbox and then i need to check if atlest one is checked.

    below is my code for ur better understanding

    thankx in advance

    <%if(col!=null){
    %>
    <td><input type ="checkbox" name="check" id="check" ></td>

  2. #2
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not 100% sure what your asking but to test a checkbox you do something like this:

    if (checkbox_name.checked) {

    }

    of course you have to use document.forms[form_number] to be able to finds the checkbox along with its name, OR pass the form object to the function OR set an ID on the checkboxes (usually same value as the name) like this:

    <input type='checkbox' name='but' id='but' />

    then you can use DOM based code to find and test it like this:

    if (document.getElementById('but').checked) {

    }

    hope that helps ........ as i said Im not 100% sure what you're asking

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1) First, you need to see if the checkbox exists:
    Code:
    var checkbox = document.getElementById("check");
    if(checkbox)
    {
    	...
    	...
    }
    2) Then, you need to see if the .checked property is true:
    Code:
    var checkbox = document.getElementById("check");
    if(checkbox  && checkbox.checked==true)
    {
    	//do something
    	
    }
    3) If you have multiple checkboxes, and you need to check if at least one is checked, you can obtain an array of all the <input> elements in your form:
    Code:
    var inputs = document.forms["my_form"].getElementsByTagName("input");
    4) Then, you need to use a for-loop to step through the array of input elements and grab the checkboxes:
    Code:
    var inputs = document.forms["my_form"].getElementsByTagName("input");
    for(var i = 0, len = inputs.length; i < len; i++)
    {
    	if (inputs[i].type=="checkbox" && inputs[i].checked==true)
    	{
    		
    	}
    }
    Or, something that may be easier is to give all the checkboxes the same name, and then you can obtain an array of just those checkboxes:
    Code:
    var checkboxes = document.getElementsByName("check");
    for(var i = 0, len = checkboxes.length; i < len; i++)
    {
    	if(checkboxes[i].checked == true)
    	{
    
    	}
    }
    5) Finally, you can set a flag when a checkbox is checked:

    Code:
    var inputs = document.forms["my_form"].getElementsByTagName("input");
    var is_one_checked = false;
    for(var i = 0, len = inputs.length; i < len; i++)
    {
    	if (inputs[i].type=="checkbox" && inputs[i].checked==true)
    	{	
    		is_one_checked = true;
    		
    	}
    }
    Last edited by 7stud; Feb 7, 2005 at 01:20.


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
  •