Welcome Guest!

If you do not have an account yet on The Web Squeeze forums, please Register! It’s FREE and there are many benefits:

  • Receive Fast Advice
  • Learn Programming Languages
  • Get Professional Website Reviews
  • Quick Troubleshooting Assistance

> Document.forms.myform Has No Properties?

This is a discussion on Document.forms.myform Has No Properties?, within the Javascript section. This forum and the thread "Document.forms.myform Has No Properties?" are both part of the Programming Your Website category.

 
Reply to this topicStart new topic
> Document.forms.myform Has No Properties?
Jason
post Apr 23 2008, 05:32 AM
Post #1


Master of the Universe
******

Group: Mentor
Posts: 1,094
Joined: 15-February 08
From: London, England
Member No.: 141



Could someone explain to me what this error is and why it is being generated please.

I have a form like such.

CODE
<form method="post" enctype ="multipart/form-data" name="myForm">


In the form are 3 fields. Duration, Homepage and website. Each input box contains an 'onchange="calculate()"'

This is my Javascript. I am trying to create a running total based on the input boxes.

CODE
<script type="text/javascript">
function checkFields(){
var website = document.forms['myForm'].website.value;
var homepage = document.forms['myForm'].homepage.value;
var duration = document.forms['myForm'].duration.value;

if(website == ""){
website = 0;
} else{
website = 4;
}

if(homepage == ""){
homepage = 0;
} else{
homepage = 20;
}

if(duration == ""){
duration = 0;
}

var total_value = website + homepage + duration;
}

// then on calculate() do something like this:
function calculate(){
var total = 100; // enter whatever you want the total to be
checkFields();
var new_total = total - total_value;
document.getElementById("showNewCalculations").innerHTML=new_total;
}
</script>


I am getting this error.

Error: document.forms.myForm has no properties

On this line.

'function checkFields(){'

Could someone tell me what is going on here. If you require any more information tell me what you need and I will put it up for you.


--------------------
Go to the top of the page
 
+Quote Post
Rakuli
post Apr 23 2008, 06:00 AM
Post #2


Squeeze Machine
*****

Group: Team Leaders
Posts: 568
Joined: 13-February 08
From: Catching the squeezed drips downunder.
Member No.: 13



Are you using a strict doctype? If yes, then this is your problem. The name attribute on forms has been deprecated in strict XHTML.

Or...

When you are accessing forms by their name you use singular rather than plural

CODE
var website = document.form['myForm'].website.value;
//or
var website = document.form['myForm'].elements['website'].value;


Or you can try giving your form an id and accessing the elements through the elements array

CODE
var website = document.getElementById('myForm').elements['website'].value;

// or assuming each field has an id

var website = document.getElementById('website').value;


Hope that helps.


--------------------
Bright Idea? -- Don't Let it disappear
Go to the top of the page
 
+Quote Post
Jason
post Apr 23 2008, 08:51 AM
Post #3


Master of the Universe
******

Group: Mentor
Posts: 1,094
Joined: 15-February 08
From: London, England
Member No.: 141



That is incredibly helpful.

I had a problem where the total_value variable wasn't being passed to the calculate function. I found a way around it but for reference how do I return a variable with JavaScript?


--------------------
Go to the top of the page
 
+Quote Post
Jason
post Apr 23 2008, 10:51 AM
Post #4


Master of the Universe
******

Group: Mentor
Posts: 1,094
Joined: 15-February 08
From: London, England
Member No.: 141



I have put in some real values and now I am getting back 'undefined'. This is not generating any errors in error console.

CODE
function calculate() {
var website = document.getElementById('website').value;
var homepage = document.getElementById('homepage').value;
var duration = document.getElementById('topad').value;

if(website == "") {
website = 0;
} else{
website = 6;
}

if(homepage == "") {
homepage = 0;
} else{
homepage = 20;
}

if(duration == "") {
duration = 0;
}
else if (duration == "3") {
duration = 50;
}
else if (duration == "7") {
duration = 50 * 2;
}

var total_value = website + homepage + duration;

document.getElementById("showNewCalculations").innerHTML='$'.total_value;


--------------------
Go to the top of the page
 
+Quote Post
Rakuli
post Apr 23 2008, 04:20 PM
Post #5


Squeeze Machine
*****

Group: Team Leaders
Posts: 568
Joined: 13-February 08
From: Catching the squeezed drips downunder.
Member No.: 13



total_value will be undefined because you are declaring website, homepage and duration within a function (I know you work with PHP so think of it like the same scope probelms you would encounter with PHP)...

If you declare a variable outside of a funtion, you can change it within and access it outside of a function. If you dclare a variable inside of a function (except arrays and objects) then you can only change it within that function and would have to return it.

You can return values simple using

CODE
return statement here;

// eg

return something + something * something


Then just call the function to set value to a variable

CODE
var = someFunction();


--------------------
Bright Idea? -- Don't Let it disappear
Go to the top of the page
 
+Quote Post
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   2 902 113 18th March 2008 - 11:00 PM
Last post by: Rakuli
No New Posts   7 djeyewater 269 2nd May 2008 - 04:07 PM
Last post by: djeyewater
No New Posts   6 HitByLife 98 20th May 2008 - 05:50 AM
Last post by: HitByLife
No new   16 mcdanielnc89 367 9th July 2008 - 08:39 PM
Last post by: mcdanielnc89
No New Posts   5 mv08jml 124 18th September 2008 - 09:01 AM
Last post by: mv08jml