0

i have this input fields in my html template :

<form>
  <input id="id_usd_value">
  <input id="id_rer_value" value="3000">
  <input id="id_euros_value">
</form>

And this jquery code that it's supossed to update the usd value if i change pesos value or viceversa depending of the rer value :

<script type="text/javascript" src="jquery-2.0.3.min.js" charset="UTF-8" ></script>
    <script type="text/javascript">
    $(document).ready(
      function() {
        $('#id_usd_value').on('change',
          function(e) {
            $('#id_pesos_value').val(
              Math.round(
                parseFloat($(this).val()) / parseFloat($('#id_rer_value').val())
              ) 
            );
          }
        );

        $('#id_pesos_value').on('change',
          function(e) {
            $('#id_usd_value').val(
              Math.round(
                parseFloat($(this).val()) * parseFloat($('#id_rer_value').val())
              ) 
            );
          }
        );
      }
    );
    </script>

But it's not working, can you please help me ??

5
  • 1
    <input id="id_euros_value"> vs $('#id_pesos_value')...? Is this a typo? Commented Jan 5, 2016 at 14:59
  • Excuse me, im a noob on jquery, what's a typo ?? Commented Jan 5, 2016 at 15:08
  • You don't have an input with the ID id_pesos_value, but you reference that in your jQuery. Is <input id="id_euros_value"> supposed to be <input id="id_pesos_value">? See @Yoeri's answer Commented Jan 5, 2016 at 15:09
  • gee...you're right sorry , but still not working on the template =( Commented Jan 5, 2016 at 15:13
  • Possible duplicate of jQuery on change event is not working Commented Jan 6, 2016 at 21:51

2 Answers 2

1

You have a typo, see fiddle for working version: You wrote id_euros_value instead of id_pesos_value

<form>
  <input id="id_usd_value">
  <input id="id_rer_value" value="3000">
  <input id="id_pesos_value">
</form>

https://jsfiddle.net/2x9r9a2v/

Sign up to request clarification or add additional context in comments.

7 Comments

Im so sorry...thanks, i wrote it right now but sill don't work, any idea why ?
In fiddle works fine, in the code snippet too...but in my template when i change any field value nothing happens....and i already corrected the code id's
Are you sure you included JQuery? Do you include this script as a js or is it inline script? Can you post your includes etc?
It's inline script, the only call im doing to jquery it's on the script label: <script type="text/javascript" src="jquery-2.0.3.min.js" charset="UTF-8" ></script>
if you open the a network inspector, do you get 200 or 304 on the query file?
|
0

Here's the solution!

jQuery(document).ready(function($) {
  
  $('#id_usd_value').on('change',
    function(e) {
      $('#id_euros_value').val(
        Math.round(
          parseFloat($(this).val()) / parseFloat($('#id_rer_value').val())
        )
      );
    }
  );

  $('#id_pesos_value').on('change',
    function(e) {
      $('#id_usd_value').val(
        Math.round(
          parseFloat($(this).val()) * parseFloat($('#id_rer_value').val())
        )
      );
    }
  );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<form>
  <input id="id_usd_value" type="number" placeholder="USD">
  <input id="id_rer_value" type="number" value="3000"  placeholder="USD">
  <input id="id_euros_value" type="number" placeholder="EUR" >
</form>

3 Comments

Sry @Yoeri i was writing the answer while you ended up first eheh
if you run the code snippet and you put 100000 on the first field and then u click outside (this is when the 'change' event is triggered) you see 33 on the third field and that's what u supposed to do (100000 / 3000 = 33)
Yes, and if i try it on fiddle as @yoeri suggest it works too, but in my template doesn't

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.