/    Sign up×
Community /Pin to ProfileBookmark

HTML help please

I am trying to create a page where you input how many miles you traveled and gallons of gas used, then press the button. The page should display the miles per gallon. The problem is I am not getting the miles per gallon calculation. Can someone please help me. Here is my code:

<html>

<head>
<title>mpg</title>

<script type=”text/javascript”>
function mpg()
{
var milestraveled, mpg, gallonsused;

milestraveled = parseFloat (document.GASForm.milestraveled.value);

gallonsused = parseFloat (document.GASForm.gallonsused.value);

mpg = milestraveled/gallonsused;

document.GASForm.mpg.value = mpg;
}
</script>

</head>

<body>
<H2>Miles Per Gallon Calculator </H2>

<form name = “GASForm”>
Miles traveled: <input type=”text” name=”milestraveled” size=10 value=0>

Gallons used: <input type=”text” name=”gallonsused” size=10 value=0>

<input type=”button” value=”Total Miles per gallon used” onclick=”mpg()”>

<input type=”text” name=”mpg” size=10>
</form>

</body>

</html>

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@_Aerospace_Eng_Nov 06.2006 — Your function name can't be the same as a variable you've declared.
Copy linkTweet thisAlerts:
@cbcatanzauthorNov 06.2006 — Your function name can't be the same as a variable you've declared.[/QUOTE]

Cool. Thanks. I changed the code, but I'm still not able to get the miles per gallon to calculate. Here is the updated code:

<html>

<head>

<title>mpg</title>

<script type="text/javascript">

function milespergallon()

{

var milestraveled, gallonsused, mpg;

milestraveled = parseFloat (document.GASForm.milestraveled.value);

gallonsused = parseFloat (document.GASForm.gallonsused.value);

mpg = milestraveled/gallonsused;

document.GASForm.mpg.value = mpg;

}

</script>

</head>

<body>

<H2>Miles Per Gallon Calculator </H2>

<form name = "GASForm">

Miles traveled: <input type="text" name="milestraveled" size=10 value=0>

Gallons used: <input type="text" name="gallonsused" size=10 value=0>

<input type="button" value="Total Miles per gallon used" onclick="mpg()">

<input type="text" name="mpg" size=10>

</form>

</body>

</html>
Copy linkTweet thisAlerts:
@cbcatanzauthorNov 06.2006 — I figured it out. Thanks, _Aerospace_Eng_. The correct code is:

<html>

<head>

<title>mpg</title>

<script type="text/javascript">

function mpgfunc()

{

var milestraveled, mpg, gallonsused;

milestraveled = parseFloat (document.GASForm.milestraveled.value);

gallonsused = parseFloat (document.GASForm.gallonsused.value);

mpg = milestraveled/gallonsused;

document.GASForm.mpg.value = mpg;

}

</script>

</head>

<body>

<H2>Miles Per Gallon Calculator </H2>

<form name = "GASForm">

Miles traveled: <input type="text" name="milestraveled" size=10 value=0>

Gallons used: <input type="text" name="gallonsused" size=10 value=0>

<input type="button" value="Total Miles per gallon used" onclick="mpgfunc()">

<input type="text" name="mpg" size=10>

</form>

</body>

</html>
×

Success!

Help @cbcatanz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...