Results 1 to 2 of 2

Thread: problem determining whether checkbox is on or off

  1. #1
    Join Date
    Jul 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default problem determining whether checkbox is on or off

    All I want to do is determine whether the selected checkbox is on or off. At the moment, when the checkbox is ticked or unticked the javascript returns false. Any help much appreciated.

    Code:
    <form name="update_active" method="POST">
    <input type="checkbox" name="active" onClick="updateActive(1, 'product')"  />1
    <input type="checkbox" name="active" onClick="updateActive(2, 'product')"  />2
    <input type="checkbox" name="active" onClick="updateActive(3, 'product')"  />3
    <input type="checkbox" name="active" onClick="updateActive(4, 'product')"  />4
    </form>
    Code:
    //is checkbox on or off?
    function updateActive(id, type)
    {
    	//is checkbox on or off?
    	for (var i=0; i < document.update_active.active.length; i++)
    	{
    		
    	if (document.update_active.active[i].checked){
    		var active = 'y';	
    	} else {
    		var active = 'n';	
    	}
    	
       }
       alert(active);
    }
    Last edited by thetestingsite; 07-03-2009 at 04:08 AM. Reason: fixed code tag

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    This should do it:
    Code:
    <script type="text/javascript">
    var active = new Array();
    var updateActive = function(key, me){
      if(me.checked == true){
        active[key] = true;
      } else {
        active[key] = false;
      }
      alert(active[key]);
    };  
    </script>
    <input type="checkbox" onClick="updateActive(1, this)" />
    <input type="checkbox" onClick="updateActive(2, this)" />
    active[key] is the result of it being checked or not.

    Good Luck!
    Jeremy | jfein.net

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
  •