/    Sign up×
Community /Pin to ProfileBookmark

Trying to Grab Values from Form and Calculate Output into Span Tag?

This is my first javascript program. I’ve been studying for a few days and thought I’d put it into practice.

The problem is I’m trying to grab 2 numbers and multiply them. This will be a program to calculate percentage of something. I’m testing if two numbers can be multiplied and the output inserted into a span tag with id=”total”. When I hit calculate, it prints the total in span tag but quickly refreshes with default value over the total value.

[CODE] <form method=”get” action=””>
<fieldset>
<legend>Find Percentage of a Number</legend>
<ul>
<li>
<label for=”base”>Base:</label>
<input type=”text” id=”base” />
</li>
<li>
<label for=”percent”>Percent:</label>
<input type=”text” id=”percent” />
</li>
<li>
<p><span id=”total”>Total?</span></p>
</li>
<li>
<input type=”submit” id=”submit” value=”Caculate?” onclick=”mainInit()” />
</li>
</ul>
</fieldset>
<script type=”text/javascript” src=”mathshop.js”></script>
</form> [/CODE]

[CODE]
function mainInit() {
if(document.getElementById) {
if(document.getElementById(“submit”).onclick) {
grabValues();
}
}
else {
alert(“Sorry, your browser does not support JavaScript.”);
}
}

function grabValues() {
var a = document.getElementById(“base”).value;
var b = document.getElementById(“percent”).value;
findTotal(a, b);
}

function findTotal(base, percent) {
var total = base * percent;
document.getElementById(“total”).innerHTML = total;

}
[/CODE]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@daihuwsAug 26.2012 — Hi!

I took the liberty of altering your code slighty:

(result here: http://pastebin.com/sbBbsQEf)

It took me a minute to realise what was going on, but what was causing that behaviour was that you've used a form method; and pressing a form submit button causes the web page to refresh itself. You don't need that with JS; you can just capture the data from the form fields and do your calculation, before returning the result to the desired location.

I've also combined the grabValues() and findTotal() functions into a single calculate() function as I think it looks tidier that way. But that's just my personal aesthetic choice. ?
×

Success!

Help @cdworks 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 5.19,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...