Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-25-2012, 04:36 AM   PM User | #1
Stefanie
New to the CF scene

 
Join Date: Jan 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Stefanie is an unknown quantity at this point
Beginner Help Please

I will start off by saying yes this is homework but I only want help. I'm trying to make the content show up in the read only boxes but I can't figure out where I'm going wrong. My test page can be found at http://sidewalk-cafe.com/test/maddox-hw2a.html. It's small. Any help would be appreciated.

Code:
function orderForm() {
	var formDaysStaying = document.getElementById("daysStaying").value;
	var formSeason = document.getElementById("season").value;
	var formPropertyType = document.getElementById("propertyType").value;
    document.getElementById("FEE").value = "$" + today.toFixed(2);
    document.getElementById("totalCost").value = "$" + today.toFixed(2);	
    document.getElementById("today").value = "$" + today.toFixed(2);
    document.getElementById("due").value = "$" + due.toFixed(2);	
    }


function formTotalCost(form) {
	const FEE = 55;
    var totalCost = (formPropertyType * formSeason) * formDaysStaying;
	var today = totalCost * .10;
	var due = (totalCost + FEE) - today;
    if (document.getElementById("daysStaying").value == "" ||
    isNaN(document.getElementById("daysStaying").value))
    alert("Please provide the number of days you will be staying.");
          // Submit the order to the server
          form.submit();
}
Stefanie is offline   Reply With Quote
Old 01-25-2012, 06:00 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,565
Thanks: 62
Thanked 4,057 Times in 4,026 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Your problem is that all those variables that you declared using the var keyword in your first function are *INVISIBLE* to any other function.

They are called "local variables".

If you want a "global variable" (one visible to all functions) you *can* just omit the var keyword, but it's much better practice to declare them outside of any funciton.

So, for example:
Code:
var formDaysStaying;
var formSeason;
var formPropertyType;

function orderForm() {
    formDaysStaying = document.getElementById("daysStaying").value;
    formSeason = document.getElementById("season").value;
    formPropertyType = document.getElementById("propertyType").value;
    document.getElementById("FEE").value = "$" + today.toFixed(2);
    document.getElementById("totalCost").value = "$" + today.toFixed(2);	
    document.getElementById("today").value = "$" + today.toFixed(2);
    document.getElementById("due").value = "$" + due.toFixed(2);	
    }
    ...
What happens, though, if your second function gets called before the first one ever is invoked??

I think, as a safety measure, you might want to call the first function as the very first action in the second one.

Also... You have an alert there in the second function, but even though you tell the user something is wrong, you go ahead and submit the <form>.

Finally, if you are calling your second function from either the <form onsubmit=...> or any <input type=submit onclick=...>, you have a problem.

****

Oh, and also: It's a really bad idea to have an if that is *NOT* followed by { ... } to clearly denote what statements are going to be executed (or not) because of the if.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
Stefanie (01-25-2012)
Old 01-25-2012, 06:53 AM   PM User | #3
Stefanie
New to the CF scene

 
Join Date: Jan 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Stefanie is an unknown quantity at this point
Thank you!! I helps a lot and it finally clicks a little more. I shall now stare at my screen some more.
Stefanie is offline   Reply With Quote
Old 01-25-2012, 07:49 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,565
Thanks: 62
Thanked 4,057 Times in 4,026 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Just one more thing: Where the heck is the today variable coming from? I think all those usages of + today.toFixed(2) have a problem???
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 01-26-2012, 05:33 AM   PM User | #5
Stefanie
New to the CF scene

 
Join Date: Jan 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Stefanie is an unknown quantity at this point
Soooo apparently I'm still an idiot cause this still isn't clicking for me. Please help again.

http://www.sidewalk-cafe.com/test/maddox-hw2a.html
Stefanie is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:24 PM.


Advertisement
Log in to turn off these ads.