Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript help

Status
Not open for further replies.

IamScott

Technical User
Jan 3, 2007
4
US
All,

Below find my JS which is supposed to do the following:
Take user supplied numerical values from 7 fields, perform the calculation function that's in the code and place it into the Total box / field.

Needless to say ...or I wouldn't be here...it doesn't work.
I thought maybe the problem was in the function, but I'm not sure

Thanks to all who can help!

NewbieScott

================================

<html>
<head>
<title>Test Prototype</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
* {
margin:0;
padding:0;
}
body {
background:#fff;
}
p {
margin:2em;
}
</style>
<script type="text/javascript">

function calc(D1,D2,D3,D4,D5,D6,D7){

return (((D1*2.036+D3)/(273+D5))-((D2*2.036+D4)/(273+D6)))*238*(3050/D7);


var D1=parseInt(D1.value);
var D2=parseInt(D2.value);
var D3=parseInt(D3.value);
var D4=parseInt(D4.value);
var D5=parseInt(D5.value);
var D6=parseInt(D6.value);
var D7=parseInt(D7.value);
if (isNaN(D1)) {D1=0;}
if (isNaN(D2)) {D2=0;}
if (isNaN(D3)) {D3=0;}
if (isNaN(D4)) {D4=0;}
if (isNaN(D5)) {D5=0;}
if (isNaN(D6)) {D6=0;}
if (isNaN(D7)) {D7=0;}
document.getElementById('total').value = D1+D2+D3+D4+D5+D6+D7;
}

window.onload=function() {
var D1=document.getElementById('D1');
var D2=document.getElementById('D2');
var D3=document.getElementById('D3');
var D4=document.getElementById('D4');
var D5=document.getElementById('D5');
var D6=document.getElementById('D6');
var D7=document.getElementById('D7');
D1.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7);
D2.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7);
D3.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7);
D4.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7);
D5.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7);
D6.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7);
D7.onkeyup=function() {calc(D1,D2,D3,D4,D5,D6,D7);
}
</script>
</head>
<body>
<form action="#">
<p><label> Number 1: <input id="D1"></label>
<p><label> Number 2: <input id="D2"></label>
<p><label> Number 3: <input id="D3"></label>
<p><label> Number 4: <input id="D4"></label>
<p><label> Number 5: <input id="D5"></label>
<p><label> Number 6: <input id="D6"></label>
<p><label> Number 7: <input id="D7"></label>


<p>Total = <input id="total"></p>
</p>
</body>
</html>
 
Breaking it down:

"return" tells the script to exit the function so everything after this line does not get executed.
Code:
return (((D1*2.036+D3)/(273+D5))-((D2*2.036+D4)/(273+D6)))*238*(3050/D7)

You are missing the closing squigglies

Code:
D1.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7)[COLOR=red]}[/color];
D2.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7)[COLOR=red]}[/color];
D3.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7)[COLOR=red]}[/color];
D4.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7)[COLOR=red]}[/color];
D5.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7)[COLOR=red]}[/color];
D6.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7)[COLOR=red]}[/color];
D7.onkeyup=function() {calc(D1,D2,D3,D4,D5,D6,D7)[COLOR=red]}[/color];

Don't know exactly what you want to do with that calculation in the first code box, but try this (to sum up the values and put the result in the total box:

I removed that calculation, but you should be able to figure out where to put it if you need it.

JavaScript:
function calc(D1,D2,D3,D4,D5,D6,D7){

var D1=parseInt(D1.value);
var D2=parseInt(D2.value);
var D3=parseInt(D3.value);
var D4=parseInt(D4.value);
var D5=parseInt(D5.value);
var D6=parseInt(D6.value);
var D7=parseInt(D7.value);
if (isNaN(D1)) {D1=0;}
if (isNaN(D2)) {D2=0;}
if (isNaN(D3)) {D3=0;}
if (isNaN(D4)) {D4=0;}
if (isNaN(D5)) {D5=0;}
if (isNaN(D6)) {D6=0;}
if (isNaN(D7)) {D7=0;}
document.getElementById('total').value = D1+D2+D3+D4+D5+D6+D7;
}

window.onload=function() {
var D1=document.getElementById('D1');
var D2=document.getElementById('D2');
var D3=document.getElementById('D3');
var D4=document.getElementById('D4');
var D5=document.getElementById('D5');
var D6=document.getElementById('D6');
var D7=document.getElementById('D7');
D1.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7)};
D2.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7)};
D3.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7)};
D4.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7)};
D5.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7)};
D6.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7)};
D7.onkeyup=function() {calc(D1,D2,D3,D4,D5,D6,D7)};
}



--------

GOOGLE is a great resource to find answers to questions like "how do i..."


--------
 
On a sidenote - check out jQuery for much cleaner code and better access to DOM elements.

That aside, there is no need to declare variables when you are passing them in to the function (function calc). Remove the var declaration.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top