/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Working with an experimental Javascript form

1) How do I run a script without the page reloading and losing the values?
2) How can I use Javascript to get text from a textarea in a form?

Here’s my HTML

[code=html]<?xml version=”1.0″ encoding=”utf-8″?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
<html xml:lang=”en” xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”application/xhtml+xml; charset=utf-8″ />
<meta http-equiv=”Content-Style-Type” content=”text/css” />
<meta http-equiv=”Content-Script-Type” content=”text/javascript” />
<title>JavaScript Calculator</title>
<link type=”text/css” rel=”stylesheet” href=”./Calculator.css” />
</head>

<body>
<h1>Javascripted Calculator</h1>
<form action=”./Calculator.xhtml” id=”Calculator” method=”post”>
<fieldset id=”Numbers”>
<legend>Enter Values Here</legend>
<ul>
<li id=”Number1″><label>First Number:</label><input type=”text” id=”Value1″ /></li>

<li id=”Number2″><label>Second Number:</label><input type=”text” id=”Value2″ /></li>
</ul>
</fieldset>
<fieldset id=”Operations”>
<legend>Choose Operation</legend>
<ul>
<li id=”Add”><button>+</button></li>

<li id=”Sub”><button>-</button></li>
<li id=”Mul”><button>*</button></li>
<li id=”Div”><button>/</button></li>
</ul>
</fieldset>
<div id=”Result” />
</form>
<script type=”text/javascript” src=”Calculator.js” />
</body>
</html>[/code]

Here’s my Javascript.

[code=php]document.getElementById(‘Add’).getElementsByTagName(‘button’)[0].onclick = function(){Calculate(‘Add’);}
document.getElementById(‘Sub’).getElementsByTagName(‘button’)[0].onclick = function(){Calculate(‘Sub’);}
document.getElementById(‘Mul’).getElementsByTagName(‘button’)[0].onclick = function(){Calculate(‘Mul’);}
document.getElementById(‘Div’).getElementsByTagName(‘button’)[0].onclick = function(){Calculate(‘Div’);}

function Calculate(Operation){
Op = Operation;
var Num1 = document.getElementById(‘Value1’).data;
var Num2 = document.getElementById(‘Value2’).data;
var Num3;
switch (Op){
case ‘Add’:
Num3 = Num1 + Num2;
break;
case ‘Sub’:
Num3 = Num1 – Num2;
break;
case ‘Mul’:
Num3 = Num1 * Num2;
break;
case ‘Div’:
Num3 = Num1 / Num2;
break;
}
document.getElementById(‘Result’).appendChild(document.createTextNode(Num3));
}[/code]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@ZeroKilledNov 15.2007 — very interesting. i don't know what purpose serve the tag <button> when <input type='button'> do the same. however, that was what i thought until now. when a button tag is contained within a form and the type isn't specified, by default the type is 'submit', hence will act as submiting form to specified form's action. what is interesting is that msie don't act like that, other than event it just do nothing (or at least with your sample). i would specify the type to button, or change the tag to input type button.

note: your code still don't work, but instead of fixing the code i will leave it to you. have fun
Copy linkTweet thisAlerts:
@Mr_Initial_ManauthorNov 15.2007 — I thought changing from input to button, it would keep the page from refreshing, but it didn't.
Copy linkTweet thisAlerts:
@mrhooNov 15.2007 — <form action="./Calculator.xhtml" id="Calculator" method="post" onsubmit="return false">

and <button type="button">
Copy linkTweet thisAlerts:
@yearbassNov 15.2007 — for the calculation function, this some correction

var Num1 = parseFloat(document.getElementById('Value1').value);

var Num2 = parseFloat(document.getElementById('Value2').value);
Copy linkTweet thisAlerts:
@Mr_Initial_ManauthorNov 15.2007 — HERE WE GO!

[code=php]document.getElementById('Result').appendChild(document.createTextNode(''));
var Num1;
var Num2;
var Num3;
var round_to_decimal_places = 4;
var rounding_num = 10;
for(i=0; i < (round_to_decimal_places - 1); i++){rounding_num *= 10}
document.getElementById('Add').getElementsByTagName('input')[0].onclick = function(){Calculate('Add');}
document.getElementById('Sub').getElementsByTagName('input')[0].onclick = function(){Calculate('Sub');}
document.getElementById('Mul').getElementsByTagName('input')[0].onclick = function(){Calculate('Mul');}
document.getElementById('Div').getElementsByTagName('input')[0].onclick = function(){Calculate('Div');}

function Calculate(Operation){
Num1 = parseFloat(document.getElementById('Value1').value);
Num2 = parseFloat(document.getElementById('Value2').value);
switch (Operation){
case 'Add':
Num3 = Num1 + Num2;
break;
case 'Sub':
Num3 = Num1 - Num2;
break;
case 'Mul':
Num3 = Num1 * Num2;
break;
case 'Div':
Num3 = Num1 / Num2;
break;
}
if(Num3 == 'NaN'){
Num3 = "Please enter a number in both textboxes.";
}
document.getElementById('Result').firstChild.nodeValue = Num3;
}
document.getElementById('Clear').onclick = function(){
document.getElementById('Result').firstChild.nodeValue = '';
document.getElementById('Value1').nodeValue = '';
document.getElementById('Value2').nodeValue = '';
}[/code]


[code=html]<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<title>JavaScript Calculator</title>
<link type="text/css" rel="stylesheet" href="./Calculator.css" />
</head>
<body>
<h1>Javascripted Calculator</h1>
<form action="./Calculator.xhtml" id="Calculator" method="post">
<fieldset id="Numbers">
<legend>Enter Values Here</legend>
<ul>
<li id="Number1"><label>First Number:</label><input type="text" id="Value1" /></li>
<li id="Number2"><label>Second Number:</label><input type="text" id="Value2" /></li>
</ul>
</fieldset>
<fieldset id="Operations">
<legend>Choose Operation</legend>
<ul>
<li id="Add"><input type="button" value="+" /></li>
<li id="Sub"><input type="button" value="-" /></li>
<li id="Mul"><input type="button" value="*" /></li>
<li id="Div"><input type="button" value="/" /></li>
</ul>
</fieldset>
<fieldset id="Clear">
<legend>Clear Values</legend>
<div><input type="reset" value="Clear" /></div>
</fieldset>
<div id="Result" />
</form>
<script type="text/javascript" src="Calculator.js" />
<p id="Valid"><img src="http://www.w3.org/Icons/valid-xhtml11" alt="Valid XHTML 1.1" /> <img src="http://validator.w3.org/images/valid_icons/valid-css.gif" alt="Valid CSS" /></p>
</body>
</html>[/code]


body *{
position:absolute;
}
h1{
margin:0;
text-align:center;
height:50px;
line-height:50px;
left:5px;
right:5px;
top:5px;
border:1px solid black;
padding:5px;
}
#Calculator{
top:100px;
left:5px;
right:5px;
border:1px solid green;
height:200px;
}
fieldset{
border:1px solid blue;
height:120px;
top:5px;
}
#Numbers{
left:5px;
width:400px;
}
legend{
text-align:right;
color:blue;
}
label{
display:block;
float:left;
width:120px;
}
input[type='text']{
display:block;
}
li{
clear:both;
list-style-type:none;
height:30px;
margin:0;
padding:0;
left:0;
}
#Number1{top:0px;
}
#Number2{top:40px;}

#Number1 label, #Number2 label{
top:0;
left:0;
}
#Number1 input, #Number2 input{
top:0;
left:125px;
}
ul{
margin:0 0 0 40px;
padding:0;
}
#Operations{
left:440px;
width:30px;
}
#Add{top:0;}
#Sub{top:25px;}
#Mul{top:50px;}
#Div{top:75px;}
#Result{
padding:5px;
line-height:30px;
height:30px;
border:1px solid teal;
top:150px;
left:7px;
right:7px;
}
#Clear{
border:1px solid olive;
left:600px;
width:100px;
height:120px;
}
#Clear legend{
color:olive;
}
#Clear div{
top:0px;
left:0px;
right:15px;
height:100px;
margin:auto;
border:1px solid black;
}
#Clear input{
left:2px;
right:2px;
top:2px;
bottom:2px;
}
×

Success!

Help @Mr_Initial_Man 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.16,
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,
)...