Click to See Complete Forum and Search --> : Working out Percentage


jwalker80
06-09-2006, 05:10 AM
Hi,

I am going to use javascript to calculate a value depending on what the user selects from a dropdown menu.

I have 2 columns in a table and a dropdown menu. The value in the 2nd column is going to be calculated by taking a percentage of the number that is hardcoded in the 1st column.

The user will select an option from the drop down e.g. 10% 20% 30% etc... and the value in the 2nd column will be calculated according to the percentage of the value in the 1st column.

Anyone know a simple way to do this??

sridhar_423
06-09-2006, 05:32 AM
<td id="one1">100</td>
<td>
<select name="ss" onchange="one2.innerHTML=one1.innerHTML*this.value/100">
<option value=0>0%</option>
<option value=10>10%</option>
<option value=20>20%</option>
</select></td>
<td id="one2"></td>

Kor
06-09-2006, 05:34 AM
onchange="document.getElementById('one2').innerHTML=document.getElementById('one2').innerHTML*this.value/100">

Learn to code crossbrowser sridhar_423, not for IE only.

sridhar_423
06-09-2006, 06:03 AM
ok sir

Kor
06-09-2006, 06:47 AM
http://www.howtocreate.co.uk/tutorials/javascript/introduction

jwalker80
06-09-2006, 10:57 PM
Thanks for that,

Thats working but im trying to round off the number so that there is no decimal. I have tried this but it's not working:

onchange="Math.round(document.getElementById('one2').innerHTML=document.getElementById('one2').innerHTML*this. value/100, 0)">

I also want to add a $ sign to the value in <td id="one1">100</td> i.e. $100 but i dont want this upset the calculation. Is the best way to do this to use a .substring to read the value without the dollar sign?

jwalker80
06-10-2006, 12:09 AM
I have got the substring fixed but I am still trying to round the number to no decmial remainder and add a dollar sign in front of the value in the last column, anyone help, im sure its simple, just havent used JS really before!

See the last column in my screenshot attached

Kor
06-10-2006, 01:15 AM
use toFixed(n) method, where n is the number of desired floated digits. Your case, toFixed(0)

onchange="document.getElementById('one2').innerHTML='\u0024'+(document.getElementById('one1').innerHTML*this.v alue/100).toFixed(0)">