Click to See Complete Forum and Search --> : Problem with finding out the number of elements on a page


Tzafrir
03-17-2003, 10:10 AM
Hi guys,

I have a web page which may have one drop down menu or more and they all have the same name being Mark. When there are a few of the drop down menus I get the correct number but when there is only one the number of options in the drop down menu appear what am i doing wrong, here is the javascript im using:

<script language ="JavaScript">
<!-- Begin hiding here --
function alertbox(x)
{
var e = "";
var r = true;
var t = true;
var s = 0;
s = x.Mark.length
if(x.Password.value == "")
{
e += "\nPlease enter a Password";
r = false;
}
if(eval(x.Choice[0].checked)!==true && eval(x.Choice[1].checked)!==true)
{
e += "\nPlease select whether you would like the Save or Submit the Mark sheet";
r = false;
}
if(eval(x.Choice[1].checked)==true)
{
for(var i=0;i<s;i++)
{
if(eval(x.Mark[i].value==""))
{
e += "\nPlease enter grade for student "+ x.Number[i].value +" before submitting";
r = false;
}
}
}
if(r == false)
{
alert("The following must be completed first:\n"+e);
}
return r;
}
// -- End hiding here -->
</script>

Can anyone help ??

Thanks

Tzaf

requestcode
03-17-2003, 10:27 AM
If you are looking for the number of elements in a form then you would do this:
form_len=document.form_name.length

form_name would be the name you gave the form. If you only have one form then you could use this format:
form_len=document.forms[0].length

"forms[0]" being the first form.

Tzafrir
03-17-2003, 10:41 AM
Im looking for the number of elements in the page with the name Mark. I tried both the things suggested in the previous post but neither helped

thanks
tzaf

Awaiting any other suggestions....

Tzafrir
03-17-2003, 10:59 AM
If it makes it easier I have hidden variables with the name Number how do I find out how many of these there are on the page?????

Thanks
Tzaf

Nicodemas
03-17-2003, 11:02 AM
I conjured up this little script which I am sure you can accomodate into your script above....


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<SCRIPT LANGUAGE="javascript">
function countMark(which){
var len = 0;
for(i=0;i<myForm.length;i++){
if(myForm.elements[i].name=="Mark"){
len+=1;
}
}
alert(len);
}
</SCRIPT>

</HEAD>

<BODY>
<FORM METHOD=POST ACTION="" NAME="myForm">
<INPUT TYPE="text" NAME="Mark"><INPUT TYPE="radio" NAME="Mark"><INPUT TYPE="radio" NAME="Mark"><INPUT TYPE="text" NAME="Jack">
<BR>
<INPUT TYPE="button" onClick="countMark()" VALUE="PRESS ME">
</FORM>
</BODY>
</HTML>


The function countMark() sets a variable to zero, then runs through each element in the form looking for the number of elements with the name "Mark". For each one it finds, it adds one to the number of the variable "len".

Tzafrir
03-17-2003, 11:32 AM
yeh the counting is now not a problem, but then when I come to check whether that element is empty it causes a problem,

basically there are two options save or submit, if the user choses the submit radio button the value of the Mark cannot be equal to "", but when I try find out this value it gives me a value Undefined. Is there a way to solve this???

Thanks

tzaf

Tzafrir
03-17-2003, 12:20 PM
PROBLEM SORTED!!!

Basically when there is 1 element there is no array so I just print the value of Mark, if there is more than 1 I then iterate through the array.

thanks

guys

tzaf