0

this is continuation of last successful topic jquery: percentage of two numbers

First of all I want to thank you for your prompt support of previuous post. Now I would like to make my script a little bit more complicated. I want to achive the following: If I insert PRICE1 and PRICE2 to have RATE between THEM, then I can change the RATE with other value and PRICE2 to change to the corespondent value according to RATE value.

My script of calculation is close to be correct, but my low knowledge about JQuery make me to ask you where I do something wrong. Thank you for your support!

<script src="libs/jquery.min.js"></script>

<script type="text/javascript">
$(function() {
    $("#PRICE1, #PRICE2").change(function() {
    var result = parseFloat(parseFloat($("#PRICE1").val(), 10) - parseFloat($("#PRICE1").val(), 10))/ parseFloat($("#PRICE2").val(), 10) * 100;
    $('#RATE').val(result||'');
  })
  else {
   $("#PRICE1, #RATE").change(function() {
    var result = parseFloat(parseFloat($("#PRICE1").val(), 10) *  parseFloat($("#RATE").val(), 10))/ 100 + parseFloat($("#PRICE1").val(), 10);
    $('#PRICE2').val(result||'');
  })}
});
</script> 

EDITED:

THE CODE ALMOST WORKING CORRECTLY WHICH MIGHT HELP OTHERS:

$(document).ready(function(){
$("#priceOne, #priceTwo").change(function() {
  var priceOne = parseFloat($("#priceOne").val());
  var priceTwo = parseFloat($("#priceTwo").val());
  $('#Rate').val((priceTwo - priceOne) / priceOne * 100); // Set the rate
});
// If price one or the rate is changed, adjust price two.
$("#priceOne, #RATE").change(function() {
  var priceOne = parseFloat($("#priceOne").val());
  var rate = parseFloat($("#Rate").val());
  $('#priceTwo').val((priceOne * rate)/ 100 + priceOne);
});
})

Thank you everyone who help me!!!

Community
  • 1
  • 1
Sergiu Costas
  • 530
  • 4
  • 8
  • 26

1 Answers1

1

There is a else and no matching if. I'm not sure what you're trying to achieve, but some condition needs to be checked.

I'm going to try and code what it appears you need. But I'm going to rename your variables, not only because allcaps are hard to type, but unless it's a constant or a macro, they shouldn't be used.

// In ready() callback
// @NOTE - This does NO error checking for division by 0 or other NaN operations.

// If price two is changed, adjust the rate.
$("#priceTwo").change(function() {
  var priceOne = parseFloat($("#priceOne").val());
  var priceTwo = parseFloat($(this).val());
  $("#rate").val(priceTwo / priceOne); // Set the rate
});
// If price one or the rate is changed, adjust price two.
$("#rate #priceOne").change(function() {
  var priceOne = parseFloat($("#priceOne").val());
  var rate = parseFloat($("#rate").val());
  $("#priceTwo").val(priceOne * rate);
});

There are a few things about your code that needs attention:

  • parseFloat doesn't take a radix argument, the 10 you pass it is ignored.
  • parseFloat(parseFloat(... is pointless, I'm not sure why you've done this.
  • Don't use jQuery to select the same element multiple times in the same scope. Save the value and re-use it - save yourself some cycles.
  • As I mentioned, don't name your variables in all capitals unless they are some sort of constant that should never be changed, it's good to have clean style habits.
Aesthete
  • 18,622
  • 6
  • 36
  • 45
  • first part and second are very similar: first one calculate Interest Rate between Price1 and PRICE2... but the second part if I change the RATE value then PRICE 2 is generetated as INTEREST RATE OF PRICE2 – Sergiu Costas Nov 29 '12 at 10:56
  • @SergiuCostas - Ok, well I made some changes and hopefully they solve your problem. – Aesthete Nov 29 '12 at 11:14
  • thank you for your way... it seems to be logically correct. But still the script is not producing a result :( – Sergiu Costas Nov 29 '12 at 12:19
  • @SergiuCostas - Ok well it's not a code problem then, it's a logic issue, and it's really difficult to determine what you're trying to achieve. – Aesthete Nov 29 '12 at 12:24
  • My code above, if to divide in two parts, separately is working as they need bringing result, but I do not how to combine them. Your code looks very logically ok, but something missing there, I do not know what exactly but it did not work :( – Sergiu Costas Nov 29 '12 at 12:30