SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need to check if a checkbox is checked

    Quick note: I know very little about JS.

    How would I tell if a checkbox (out of ~30) are currently checked or not on form submition?

    I had this code, but it gave me an error saying add_event was not an object:
    Code:
    var lgn = document.add_event.length; 
    
    for(var i = 0; i < lgn; i++)
    { 
    	if(document.add_event[i].type == "checkbox" && document.add_event[i].checked == true)
    	{
    		alert(document.add_event[i].checked)
    	}
    }
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  2. #2
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    add_event has to be the name of your form. Is that the name you gave it in the form tag?
    <form name="add_event">

  3. #3
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <form action="send_event.jsp" method="post" onSubmit="return validForm(this)" name="add_event">
    As far as I can tell, the form should have the right name.
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  4. #4
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I managed to hack it. Here's the code for any who happen upon this post in the future:
    Code:
    function dayIsChecked()
    {
    	var lgn = document.myForm.length; 
    
    	for(var i = 0; i < lgn; i++)
    	{
    		if(document.myForm[i].type == "checkbox" && document.myForm[i].checked == true)
    		{
    			// nuttin
    		}
    		else
    		{
    			var is_false = false;
    		}
    	}
    	if(is_false == false)
    	{
    		alert("You must select a day to take off")
    		return false
    	}
    }
    <form action="somescript.ext" method="post" name="myForm" onClick="return dayIsChecked()">
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*


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
  •