Search code examples
javascripthtmlcalculatorcodepen

Uncaught SyntaxError: missing ) after argument list - and I can't see where


I was making tip calculator and i got an error

function CalcTip(){
var bill = document.querySelector('#bill').value;
var people = document.querySelector('#people').value;
var button = document.querySelector('button');
var total = bill.value/people.value;                 
document.getElementById("#last").innerHTML = "$" + total;  
total = Math.round(total * 100) / 100;

}          

button.addEventListener('click',function(){} calcTip(); );


Solution

  • You had several errors in your code.

    Here is a functional example:

    var button = document.getElementById("calculate");
    button.addEventListener('click', CalcTip);
    
    function CalcTip(){
      var bill = document.getElementById('bill');
      var people = document.getElementById('people');  
      var total = parseInt(bill.value) / parseInt(people.value);                 
      total = Math.round(total * 100) / 100;
      
      document.getElementById("last").innerHTML = "$" + total;
    }
    <button type="button" id="calculate">Calc Tip</button><br>
    
    <input type="text" id="bill"><br>
    <input type="text" id="people">this can't be 0<br>
    
    <div id="last"></div>