Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   Having trouble understanding function scope and variable scope (http://www.velocityreviews.com/forums/t937704-having-trouble-understanding-function-scope-and-variable-scope.html)

Andrew Falanga 11-22-2008 05:31 PM

Having trouble understanding function scope and variable scope
 
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
[i].type + "<BR>");
}

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

David Mark 11-22-2008 07:43 PM

Re: Having trouble understanding function scope and variable scope
 
On Nov 22, 12:31*pm, Andrew Falanga <af300...@gmail.com> wrote:
> 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]

Andrew Falanga 11-22-2008 09:23 PM

Re: Having trouble understanding function scope and variable scope
 
On Nov 22, 12:43*pm, David Mark <dmark.cins...@gmail.com> wrote:
> On Nov 22, 12:31*pm, Andrew Falanga <af300...@gmail.com> wrote:
>
> > 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?


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">


>
> > <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?
>


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

Andy


All times are GMT. The time now is 02:48 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.