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

112
Visualizações
Calculate price based on markup and markup based on price using the same input fields

I have created 3 input fields Trade, Markup and Price. I would like for the user to enter a trade and then manipulate the price either by markup or by entering the price and the markup showing a reflection of that price.

I would like the calculation to be live and to not have to submit a button everytime to view changes.

I did split the code up into 2 separate scripts but that did not make a difference and I have also changed the addEventListeners to "change" but that still did not work. I have a felling I will need to add a IF statement between the 2 results somehow.

document.getElementById("trade").addEventListener("change", calculation);
    document.getElementById("price").addEventListener("change", calculation);
    document.getElementById("markup").addEventListener("change", calculation);
    
        function calculation() {

        var trade = parseFloat(document.getElementById("trade").value);
        var markup = parseFloat(document.getElementById("markup").value);
        var price = parseFloat(document.getElementById("price").value);
        
        var markupresult = price / trade;
        var priceresult = markup * trade;
        
        
        document.getElementById("markup").value = (markupresult).toFixed(2); 
        
        document.getElementById("price").value = (priceresult).toFixed(2); 
      
    }
<div class="form-group"><label>Trade</label><input type="number" id="trade" name="trade"></div>
<div class="form-group"><label>Price</label><input type="number" id="price" name="price"></div>
<div class="form-group"><label>Markup</label><input type="number" id="markup" name="markup"></div>

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

0

Check the below snippet.

You can use a switch to change only one/vice versa.

Instead of change, you can use keyup which would provide you realtime output, (Whenever the user enter a number, the result will be calculated and shown).

document.getElementById("trade").addEventListener("change", calculation);
document.getElementById("price").addEventListener("change", calculation);
document.getElementById("markup").addEventListener("change", calculation);

function calculation(event) {
  var trade = parseFloat(document.getElementById("trade").value);
  var markup = parseFloat(document.getElementById("markup").value);
  var price = parseFloat(document.getElementById("price").value);

  var markupResult = price / trade;
  var priceResult = markup * trade;

  switch (event.target.getAttribute("id")) {
    case "markup":
      document.getElementById("price").value = priceResult.toFixed(2);
      break;

    case "price":
      document.getElementById("markup").value = markupResult.toFixed(2);
      break;
  }
}
<div class="form-group">
  <label>Trade</label><input type="number" id="trade" name="trade" />
</div>
<div class="form-group">
  <label>Price</label><input type="number" id="price" name="price" />
</div>
<div class="form-group">
  <label>Markup</label><input type="number" id="markup" name="markup" />
</div>

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