?
Solved

How to check if an html object exist

Posted on 2005-04-21
2
Medium Priority
?
500 Views
Last Modified: 2009-12-16
Let's say i have 1 to 4 dynamically generated checkboxes.  The checkboxes use the same name, and the number of checkboxes depend on some database values.  Now i want to add validation to these checkboxes.  I need to make sure at least one of them is checked, I'm thinking along these lines:

checkedFlag = 0
for (i=0;i<=3;i++)
{
   if (isObject(document.MyForm.MyChkBox[i]))  //how can i check if this is an object??
   {
        if (document.MyForm.MyChkBox[i].checked)
        {
           checkedFlag = 1
           break;
         }
    }
}
0
Comment
Question by:kingsfan76
  • 2
2 Comments
 
LVL 25

Accepted Solution

by:
archrajan earned 1000 total points
ID: 13837812
if (isObject(document.MyForm.MyChkBox[i]))  //how can i check if this is an object??
shud be

 if (document.MyForm.MyChkBox[i])  
0
 
LVL 25

Expert Comment

by:archrajan
ID: 13837849
For code to check if atleast one checkbox is checked
Here u go:
 <HTML>
<HEAD>
<SCRIPT LANGUAGE=javascript>
function atLeastOne(){
  var objForm = document.forms[0];
  var el = document.getElementsByName('frm_chk_delete[]')
  for(i=0;i<el.length;i++){
    if(el[i].checked){
       return true;
    }
  }
  alert("Please check at least one box!");
  return false;
}
</SCRIPT>
</HEAD>
<BODY>
<form name="frmYourForm" action="yourpage.htm" onSubmit="return atLeastOne()">
<INPUT type="checkbox" name="frm_chk_delete[]"><br>
<INPUT type="checkbox" name="frm_chk_delete[]"><br>
<INPUT type="checkbox" name="frm_chk_delete[]"><br>
<INPUT type="checkbox" name="frm_chk_delete[]"><br>
<INPUT type="submit" value="Submit" id=submit1 name=submit1>
</FORM>
</BODY>
</HTML>
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question