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

108
Visualizações
javascript tip calculator output not showing on html page

So I seem to have this issue a lot with my output not showing on the html, and even following along previous programs seems to give me no clue on why they never show. But I have a simple tip calculate program in javascript, I want the tip amount and tip per person to appear under the form on the html page, but whenever I hit the submit button nothing appears. I tried different event listeners (both visible in the code) one on the button and one in the javascript but it did nothing. I'm not sure if it's due to the "" around the submit in html and the '' around submit in javascript, or even if I'm supposed to use '' around the ids in the html, I saw different code using either or so I was uncertain on which to really use so I went with "" around the ids in the html file.

HTML (filename pro3):

<!DOCTYPE html>
<html>
    <head>
        <title>Project 3</title>
    </head>

    <body>
        <script src = "pro3java.js"></script>

        <h1>TIP CALUCULATOR</h1>

        <form name = 'tipForm'>
            <p>Enter bill amount:</p>
            <input type="number"name="billAmt">

            <br>

            <p>Enter tip percentage:</p>
            <input type="number" name="tipPerc">

            <br>

            <p>Enter number of people:</p>
            <input type="number" name="people">

            <br>
            <br>
            
            <button type="submit" onclick="result()">Calculate</button>
            <br>
        </form>

        <div id = 'tipResult'></div>
        
    </body>
</html>

JAVASCRIPT (filename pro3java:

const form = document.forms.tipForm;

const output = document.getElementById('#tipResult');

form.addEventListener('submit', result);

//calc
let tip = parseInt(form.bill.value) * (parseInt(form.tipPerc.value) / 100);

let amount = (parseInt(form.bill.value) + parseInt(tip)) / form.people.value;

let total = amount.toFixed(2);

function result(e){
tipResult.textContent = `Your tip is $${tip}!`;
tipResult.textContent = `Everyone will pay $${total}!`;
} //result end
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There were a few things amiss here. You had the wrong variable for the bill (billAmt), and you had an event listener doubled up with an inline event (onsubmit="result()"). Finally, you needed to move your calculations to be inside the result() function. Otherwise, those calculations only happen when the page loads (when they're all empty). I fixed those 3 errors and it works. Also, I added in a 'e.preventDefault()' (e is short for event) into the form submit listener. That will prevent the form from doing it's default behaviour, which is to refresh the page

const form = document.forms.tipForm;
const output = document.getElementById('#tipResult');
form.addEventListener('submit', result);

function result(e) {
  e.preventDefault(); // stop form from refreshing the page
  //calc
  let tip = parseInt(form.billAmt.value) * (parseInt(form.tipPerc.value) / 100);
  let amount = (parseInt(form.billAmt.value) + parseInt(tip)) / form.people.value;
  let total = amount.toFixed(2);
  tipResult.textContent = `Your tip is $${tip}!`;
  tipResult.textContent = `Everyone will pay $${total}!`;
  return false

} //result end
<h1>TIP CALUCULATOR</h1>
<form name='tipForm'>
  <p>Enter bill amount:</p>
  <input type="number" name="billAmt"><br>
  <p>Enter tip percentage:</p>
  <input type="number" name="tipPerc"><br>
  <p>Enter number of people:</p>
  <input type="number" name="people"><br>
  <button type="submit">Calculate</button>
  <br>
</form>
<div id='tipResult'></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