Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Script from HTML form help required
 User Name Remember Me? Password

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 01-20-2013, 02:27 PM PM User | #1 mos5 New to the CF scene   Join Date: Jan 2013 Posts: 3 Thanks: 0 Thanked 0 Times in 0 Posts Script from HTML form help required Hello, I am trying to create a javascript function (Please see the function below) which calculates the total cost of a trip for three different vehicles based on html form input (Please see HTML code below) but I cannot get the script to work. Any help would be appreciated. Here is the HTML code: Calculating with JavaScript and HTML Forms

In this lab lets look at some additional things that JavaScript can do, such as make calculations with user input from a form.

The Problem:

I want to take a drive from Colorado Springs to Yellowstone Park. The reported distance is 638 miles. I would like to know how much the fuel is going to cost me to make this round trip. I have three options for my transportation....Assume that gas costs on averate \$2.33 per gallon.

1. My Car that gets on average 19mpg...
2. My Motorcycle that gets on average 76 mpg...
3. My Friends hybrid that gets on average 48mpg

Fill in the form items below to get the input necessary to make the calculations:

Cost with My Car Cost with Motorcycle Cost with Hybrid

Here is my attempt at the script: function CalculateCost(){ var dist = document.getElementById("dist").value; var mycarMpg = document.getElementById("mycarMpg").value; var mtrcycMPG = document.getElementById("mtrcycMpg").value; var hybridMpg = document.getElementById("hybridMpg").value; var mycar = document.getElementById("mycar").value; var mtrcyc = document.getElementById("mtrcyc").value; var hybrid = document.getElementById("hybrid").value; mycar = dist/mycarMpg*2.33; mycar = Math.round(mycar * 100)/100; mtrcyc = dist/mtrcycMPG*2.33; mtrcyc = Math.round(mtrcyc * 100)/100; hybrid = dist/hybridMpg*2.33; hybrid = Math.round(hybrid * 100)/100; } Last edited by mos5; 01-20-2013 at 04:03 PM.. Reason: Personal information
 01-22-2013, 01:19 AM PM User | #2 sunfighter Senior Coder   Join Date: Jan 2011 Location: Missouri Posts: 3,269 Thanks: 23 Thanked 468 Times in 467 Posts Code: ```var mtrcycMPG = document.getElementById("mtrcycMpg").value; var hybridMpg = document.getElementById("hybridMpg").value;``` Should Be: Code: ```var mtrcycMPG = document.getElementById("mcmpg").value; var hybridMpg = document.getElementById("hmpg").value;``` Your IDs. --------------------------------------------------------------------- These are not needed. It's where the answers go. Code: ```var mycar = document.getElementById("mycar").value; var mtrcyc = document.getElementById("mtrcyc").value; var hybrid = document.getElementById("hybrid").value;``` Your math is wrong. You multiply first than divide. So your missing parentheses (). It's mycar = (dist/mycarMpg)*2.33; Save some time and use (dist*2.33)/mycarMpg. (dist*2.33) is a constent and can be used in all calculations. K = 638 * 2.33 = 1,486.54 so mycar = 1,486.54/mycarMpg;same for mtrcyc and hybrid. And put the lines in to write your output.

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 09:33 AM.