EDITED
Thank you for everyone who offered support... the best working script I will share with you in hope that I could help others who is looking for the same solution:
$(document).ready(function(){
$("#price1, #price2").keyup(function() {
var priceOne = parseFloat($("#price1").val());
var priceTwo = parseFloat($("#price2").val());
var rate = parseFloat($("#rate").val());
if ($("#price1").val() && $("#price2").val()){
$('#rate').val(((priceTwo - priceOne) / priceOne * 100).toFixed(2));
}
});
$("#rate").keyup(function() {
var priceOne = parseFloat($("#price1").val());
var rate = parseFloat($("#rate").val());
if ($("#rate").val() && $("#price1").val() && $("#price2").val()){
$('#price2').val(((priceOne * rate)/ 100 + priceOne).toFixed(2));
}
});
})
Also you can test it following this LINK
INITIAL QUESTION:
Please help to calculate the percentage between two numbers. I tried one way, but I did not succeed. Please tell me what is wrong, or I will appreciate if you can recommend other script which could help me
my script:
<html>
<head>
<script type="text/javascript">
$("#rate").text(function() {
var result = (parseInt(($("#price1").text(), 10) * 100)/ parseInt($("#price2").text(), 10));
if (!isFinite(result)) result = 0;
return result;
});?
</script>
</head>
<body>
<div id="price1"><label><input id="price1" type="text"></label></div>
<div id="price2"><label><input id="price2" type="text"></label></div>
<div id="rate"><label><input id="rate" type="text"></label></div>
</body>
</html>
Use like this,
Changed div class, and get input value by
val()
You have few problems,
Fixed you code
Live Demo
HTML
Javascript
use
val()
instead oftext()
for input element, use$(function(){})
to wait DOM is ready. And also don't use same ID to elements.May be you want to use parseFloat() instead of parseInt()
Check this fiddle
http://jsfiddle.net/j3WY3/5/
Have made some corrections:
?
as everyone says..text()
to.val()
div
andinput
had same id