Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
What is the better way to counting number on different event?

I already solve it, Here is my HTML code 👇

<strong>Product Price = $20</strong><br>
<strong>Bag Price = $10</strong><br>
<hr>
<label>Quantity of products</label>
<br>
<input type="number" id="quantity">
<br>
<input type="checkbox" id="with_bag">
<label>With a bag</label>
<br>
<p>Total Price 👇</p>
<input type="text" id="total_price" readonly>

And here is my jQuery code 👇

// Calculate total price (On Keyup)
$(document).on("keyup", "#quantity", function() {
  var quantity = $('#quantity').val();
  var content_price = $("#with_bag").is(':checked') ? 10 : 0;
  var total_price = (20 * quantity) + content_price;
  $('#total_price').val('$' + total_price.toFixed(2));
});

// Calculate total price (On Click)
$(document).on('click', '#with_bag', function(){
  var quantity = $('#quantity').val();
  var total_price = 20 * quantity;
  if(this.checked){
    total_price = (20 * quantity) + 10;
  }
  $('#total_price').val('$' + total_price.toFixed(2));
});

I just want to know, how to get these two different events (on keyup & on click) at the same function?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can make a function and track your event conditionally if this is exactly what you want.

function myFunction(event){
  var quantity = $('#quantity').val();
  if(event.type == "keyup"){
      var content_price = $("#with_bag").is(':checked') ? 10 : 0;
      var total_price = (20 * quantity) + content_price;
      $('#total_price').val('$' + total_price.toFixed(2));
  }
  else{
      var total_price = 20 * quantity;
      if($('#with_bag').is(":checked")){
        total_price = (20 * quantity) + 10;
      }
      $('#total_price').val('$' + total_price.toFixed(2));
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<strong>Product Price = $20</strong><br>
<strong>Bag Price = $10</strong><br>
<hr>
<label>Quantity of products</label>
<br>
<input type="number" onkeyup="myFunction(event);" id="quantity">
<br>
<input type="checkbox" onclick="myFunction(event);" id="with_bag">
<label>With a bag</label>
<br>
<p>Total Price 👇</p>
<input type="text" id="total_price" readonly>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda