Having trouble understanding function scope and variable scope

A

Andrew Falanga

Hello again,

I'm having trouble understanding why the element types in the form I'm
using aren't defined in my function. Below is the code (it's a
nonsense page that I'm using just to get my understanding correct).
In the function displayFormElements(), the form.type property isn't
defined, but yet the form.length was because the for loop only goes
for 3 iterations. Is there something wrong with how I'm passing the
form to the function?

Thanks for any help,
Andy


<html>
<head>
<script type="text/javascript">
function foo() {
document.write("foo");
alert("foo");
}

function displayFormElements(form) {
document.write("displayFormElements called");
var length = form.length;
for(var i = 0; i < length; i++) {
document.write("element " + i + " is " + form.type + "<BR>");
}
}

function CheckForNull() {
alert("CheckForNull called");
var formId=document.getElementById("myForm");
if(formId.elements[0].value == "") {
alert("form may not be null");
return false;
}
return true;
}

</script>
</head>
<body>

Form test
<form id="myForm" onsubmit="return CheckForNull()" action="index.html"
method="post">
<input type=text name="text1"></input>
<textarea>example text</textarea>
<input type="submit" value="submit" />
</form>
<P>
<script language="javascript" type="text/javascript">
var theform = document.getElementById("myForm");
displayFormElements(theform);
document.write("<BR>");
for(var i = 0; i < theform.length; i++) {
document.write("element " + i + " is type: " + theform.elements
.type + "<BR>");
}

</script>
</body>
</html>
 
D

David Mark

Hello again,

I'm having trouble understanding why the element types in the form I'm
using aren't defined in my function.  Below is the code (it's a
nonsense page that I'm using just to get my understanding correct).
In the function displayFormElements(), the form.type property isn't
defined, but yet the form.length was because the for loop only goes
for 3 iterations.  Is there something wrong with how I'm passing the
form to the function?

Thanks for any help,
Andy

No doctype? Why test in quirks mode?
<html>
<head>
<script type="text/javascript">
function foo() {
   document.write("foo");
   alert("foo");

}

function displayFormElements(form) {
   document.write("displayFormElements called");
   var length = form.length;

var length = form.elements.length;
   for(var i = 0; i < length; i++) {
      document.write("element " + i + " is " + form.type + "<BR>");

Well, this is just nonsense. What do you expect the "type" property
of the form to be?
   }

}

function CheckForNull() {
   alert("CheckForNull called");
   var formId=document.getElementById("myForm");

var formId = window.document.forms.myForm;
// TODO: change variable name to something appropriate
   if(formId.elements[0].value == "") {
      alert("form may not be null");
      return false;
   }

What does that mean?
   return true;

}

[snip]
 
A

Andrew Falanga

No doctype?  Why test in quirks mode?

You know, I've been to a couple of different tutorials for javascript
online and every code example I see simply has what's down there. My
"actual" code files start with this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/
TR/html4/strict.dtd">

var length = form.elements.length;


Well, this is just nonsense.  What do you expect the "type" property
of the form to be?

LOL!! :) It's so annoying when I make simple mistakes like that. I
didn't notice that, but that was the problem.

Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top