Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

316
Vistas
how can i remove onkeyup on my javascript code

when i try to add a specific value with value="444" on the usd input it does not convert to the other value eth it's only working when i enter manually the amount. how can i convert when it's value already specified in the code

<!DOCTYPE html>
<html>
<head>
    <style>
        
    </style>
    <script src="/scripts/snippet-javascript-console.min.js?v=1"></script>
</head>
<body>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="number" name="eth" class="currencyField" placeholder="ETH">
      <div class="arrow" style="margin: 0 10px";>=</div>
      <input type="number" name="usd" value="444" class="currencyField" value="" placeholder="USD">
    </div><span id="price"></span>
    <script type="text/javascript">
         $(".currencyField").keyup(function(){ //input[name='calc']
 let convFrom;
 if($(this).prop("name") == "eth") {
       convFrom = "eth";
       convTo = "usd";
 }
 else {
       convFrom = "usd";
       convTo = "eth";
 }
 $.getJSON( "https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=ethereum", 
    function( data) {
    var origAmount = parseFloat($("input[name='" + convFrom + "']").val());        
    var exchangeRate = parseInt(data[0].current_price);
    let amount;
    if(convFrom == "eth")
       amount = parseFloat(origAmount * exchangeRate);
    else
       amount = parseFloat(origAmount/ exchangeRate); 
    $("input[name='" + convTo + "']").val(amount.toFixed(2));
    price.innerHTML = amount
    });
});
    </script>
</body>
</html>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Try this

   <script type="text/javascript">
      $(document).ready(function () {
        function getJSONData() {
          if ($(this).prop("name") == "eth") {
            convFrom = "eth";
            convTo = "usd";
          } else {
            convFrom = "usd";
            convTo = "eth";
          }
          $.getJSON("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=ethereum", function (data) {
            var origAmount = parseFloat($("input[name='" + convFrom + "']").val());
            var exchangeRate = parseInt(data[0].current_price);
            let amount;
            if (convFrom == "eth") amount = parseFloat(origAmount * exchangeRate);
            else amount = parseFloat(origAmount / exchangeRate);
            $("input[name='" + convTo + "']").val(amount.toFixed(2));
            price.innerHTML = amount;
          });
        }

        getJSONData();

        $(".currencyField").keyup(function () {
          //input[name='calc']

          getJSONData();
        });
      });
    </script>

First of all, you must change the function out of keyup, because we don't do it. The function works when you enter value.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda