# Thread: [Help] Simple javascript formula

1. Member
Join Date
Jun 2012
Posts
5

## [Help] Simple javascript formula

Hi,

I'm currently trying to do a formula calculation for my form, but for some reason I cannot seem to get it to work.

basically my formula is (H x W x L) / 166

Code:
```function calculateText(){

var op1=document.getElementById('height');
var op2=document.getElementById('width');
var op3=document.getElementById('length');
var result=document.getElementById('total');

if(op1.value=="" || op1.value!=parseFloat(op1.value)) op1.value=0;

if(op2.value=="" || op2.value!=parseFloat(op2.value)) op2.value=0;

if(op3.value=="" || op3.value!=parseFloat(op3.value)) op3.value=0;

result.value=0;

result.value=parseInt(result.value);

result.value=parseInt(result.value)+parseInt(op1.value) * (op2.value) * (op3.value) / 166);}```

2. Member
Join Date
Jun 2012
Posts
5
What am I doing wrong?

3. Registered User
Join Date
Oct 2010
Location
Versailles, France
Posts
1,290
Javascript tends to make concatenation rather than additions. Then it's always careful to begin with the multiplications to force Number conversions...
Code:
```function calculateText(){
var op1=parseFloat(document.getElementById('height'));
0var op2=parseFloat(document.getElementById('width'));
var op3=parseFloat(document.getElementById('length'));
var result=op1*op2*op3/166;
if (!result || isNaN(result)) {alert("The values seems erroneous...");return}
result=result.toFixed(2);
document.getElementById('resultSpan').innerHTML=result;
}```
Last edited by 007Julien; 06-18-2012 at 06:05 AM.

4. Member
Join Date
Jun 2012
Posts
5
Code:
```<script>
function calculateText(){
var op1=parseFloat(document.getElementById('height'));
var op2=parseFloat(document.getElementById('width'));
var op3=parseFloat(document.getElementById('length'));
var result=op1*op2*op3/166(document.getElementById('total'));
if (!result || isNaN(result)) {alert("The values seems erroneous...");return}
result=result.toFixed(2);
document.getElementById('resultSpan').innerHTML=result;
}
</script>```
Doesn't seem to work. I'm using this in conjunction with a form component "RSFORM!PRO" for my website. I used the example here, but all it does is add.

http://demo.rsjoomla.com/calculation-form-example

5. Registered User
Join Date
Oct 2010
Location
Versailles, France
Posts
1,290
It should work with a HTML line like this to display the result :
Code:
`<p>The result is <span id="resultSpan"></span></p>`

6. Member
Join Date
Jun 2012
Posts
5
Nope, not working at all. I've tried it, and the html code.

However this code works http://demo.rsjoomla.com/calculation-form-example
but it just adds, I need it to multiply 3 fields, and divide it by 166

7. Registered User
Join Date
Oct 2010
Location
Versailles, France
Posts
1,290
Sorry, I forgot the values, try this which makes the product...
Code:
```<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<title>Calculation</title>
<style type="text/css">
body {background-color:#000;color:#fff;}
#pge {display:block;width:300px;margin:20px auto;}
input{width:60px;text-align:center}
p{margin:0;}
</style>
<body>
<div id="pge"><fieldset><legend>Calculation</legend>
<p><Label><input id="width" type="text" onblur="calculateText()" value="0">&nbsp;Width - centim&#232;trer</label></p>
<p><Label><input id="height" type="text" onblur="calculateText()" value="0">&nbsp;Height - centim&#232;trer</label></p>
<p><Label><input id="length" type="text" onblur="calculateText()" value="0">&nbsp;Length - centim&#232;trer</label></p>
<p>&nbsp;</p>
<p>The volume is <span id="rsp">0</span> cubic centimetrer<span id="rsq"></span></p>
</fieldset>
</div>
<script type="text/javascript">
function calculateText(){
var op1=document.getElementById('height').value;
var op2=document.getElementById('width').value;
var op3=document.getElementById('length').value;
var result=op1*op2*op3;
document.getElementById('rsp').innerHTML=isNaN(result)?'undefined':result;
var s='';if (!isNaN(result) && 0<result-1) s='s';
document.getElementById('rsq').innerHTML=s;
}
</script>
</body>
</html>```
Good luck to divide by 166 with a debugger to see what is not working !
Last edited by 007Julien; 06-20-2012 at 07:19 AM.

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

"

"

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.13292 seconds
• Memory Usage 2,905KB
• Queries Executed 15 (?)
Template Usage (33):
• (5)bbcode_code
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (7)memberaction_dropdown
• (1)navbar
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (7)postbit
• (7)postbit_onlinestatus
• (7)postbit_wrapper
• (1)spacer_close
• (1)spacer_open
• (1)tagbit_wrapper

Phrase Groups Available (6):
• global
• inlinemod
• postbit
• posting
• reputationlevel
Included Files (26):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/functions_bigthree.php
• ./includes/class_postbit.php
• ./includes/class_bbcode.php
• ./includes/functions_reputation.php
• ./includes/functions_notice.php
• ./packages/vbattach/attach.php
• ./vb/types.php
• ./vb/cache.php
• ./vb/cache/db.php
• ./vb/cache/observer/db.php
• ./vb/cache/observer.php

Hooks Called (70):
• init_startup
• friendlyurl_resolve_class
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• strip_bbcode
• friendlyurl_clean_fragment
• friendlyurl_geturl
• forumjump
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• notices_check_start
• notices_noticebit
• process_templates_complete
• friendlyurl_redirect_canonical
• bbcode_fetch_tags
• bbcode_create
• postbit_factory
• postbit_display_start
• postbit_imicons
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• postbit_display_complete
• memberaction_dropdown
• tag_fetchbit_complete
• forumrules
• navbits
• navbits_complete