/    Sign up×
Community /Pin to ProfileBookmark

Simple example needed

Wondering if this can be done. I simply want two text boxes and a button labeled, “calculate”. User enters numeric values in the boxes and presses the button. I want the onclick function to add the values and write the result into a <div> below the form. Is it feasible to write the result to the page as text rather than as the value of a third text box? I will probably hide the <div> and then unhide it as part of the onclick event.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@FangMar 23.2005 — Basically this:
&lt;button type="button" onclick="document.getElementById('answer').firstChild.data='666';"&gt;result&lt;/button&gt;
&lt;div id="answer"&gt;answer&lt;/div&gt;

you can do the addition part. ?
Copy linkTweet thisAlerts:
@betheballauthorMar 23.2005 — you can do the addition part.[/QUOTE]

Or so I thought. Obviously I still don't fully understand the basics. Can you look at what I have and tell me where I have failed to understand?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<HTML>

<HEAD>

<TITLE>Javascript Test</TITLE>

<META http-equiv="Content-Type" content="text/html; charset=utf-8">

<script type="javascript/text">

function calculate(){

var myResult = parseFloat(document.theForm.t1.value) + parseFloat(document.theForm.t2.value)

}

</script>

</HEAD>

<BODY>

<form name="theForm" action="none">

<input type="text" name="t1" size="10" /><br />

<input type="text" name="t2" size="10" /><br />

<button type="button" onclick="document.getElementById('answer').firstChild.data=myResult;">result</button>

</form>

<div id="answer">answer</div>

</BODY>

</HTML>
Copy linkTweet thisAlerts:
@FangMar 23.2005 — &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;HTML&gt;
&lt;HEAD&gt;
&lt;TITLE&gt;Javascript Test&lt;/TITLE&gt;
&lt;META http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;script type="javascript/text"&gt;
function calculate(){
var myResult = parseFloat(document.theForm.t1.value) + parseFloat(document.theForm.t2.value)
document.getElementById('answer').firstChild.data=myResult;
}
&lt;/script&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;form name="theForm" action="none"&gt;
&lt;input type="text" name="t1" size="10" /&gt;&lt;br /&gt;
&lt;input type="text" name="t2" size="10" /&gt;&lt;br /&gt;
&lt;button type="button" onclick="calculate();"&gt;result&lt;/button&gt;
&lt;/form&gt;
&lt;div id="answer"&gt;answer&lt;/div&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;
Copy linkTweet thisAlerts:
@betheballauthorMar 23.2005 — I must have done something wrong in the function itself as both my previous version and the one you provided generate:

Line: 18

Error: Object expected
Copy linkTweet thisAlerts:
@FangMar 23.2005 — <script type="javascript/text">

should be:

<script type="text/javascript">
Copy linkTweet thisAlerts:
@betheballauthorMar 23.2005 — Thanks. Sometimes it doesn't pay to try and be compliant. I had always used language="javascript" until a few days ago when I found out it wasn't compliant.

Thanks for your always courteous assistance.
×

Success!

Help @betheball 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.18,
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,
)...