/    Sign up×
Community /Pin to ProfileBookmark

Working out Percentage

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??

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@sridhar_423Jun 09.2006 — <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>
Copy linkTweet thisAlerts:
@KorJun 09.2006 — onchange="[COLOR=Red]document.getElementById('one2')[/COLOR].innerHTML=[COLOR=Red]document.getElementById('one2')[/COLOR].innerHTML*this.value/100">

Learn to code crossbrowser [B]sridhar_423[/B], not for IE only.
Copy linkTweet thisAlerts:
@sridhar_423Jun 09.2006 — ok sir
Copy linkTweet thisAlerts:
@jwalker80authorJun 10.2006 — 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:

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

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?
Copy linkTweet thisAlerts:
@jwalker80authorJun 10.2006 — 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

[upl-file uuid=fc22bda5-92f9-4e4c-9944-944172c6c7df size=9kB]screenshot.gif[/upl-file]
Copy linkTweet thisAlerts:
@KorJun 10.2006 — use toFixed(n) method, where n is the number of desired floated digits. Your case, toFixed(0)
<i>
</i>onchange="document.getElementById('one2').innerHTML='u0024'+(document.getElementById('one1').innerHTML*this.value/100).toFixed(0)"&gt;
×

Success!

Help @jwalker80 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 5.29,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...