Making a Tip Calculator in javascript where users can enter their desired tip% and calculate button isn't working

CAAntonacci

Here is my HTML code.

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Tip Calculator</title>

<link rel="stylesheet" href="midtermcss.css">

<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"</script>

<script src="midtermJS.js"></script>

</head>

<body>
<section>
    <h1 id="heading">Tip Calculator</h1>

    <label for="billAmount">Total Amount Of Bill:</label>
    <input type="text" id="billAmount"><br>

    <label for="percentTip">Percent To Tip:</label>
    <input type="text" id="percentTip"><br>

    <label for="amountPeople">How Many People?:</label>
    <input type="text" id="amountPeople"><br>

    <label for="totalTip">Tip Total:</label>
    <input type="text" id="totalTip" disabled="disabled"><br>

    <label>&nbsp;</label>
    <input  type="button" id="calculate" value="Calculate"><br>
  </section>
 </body>
</html>

The HTML I think is fine but it shows the names of everything that I think you would need to help me understand this button situation.

This is the JavaScript portion and I know the error is in here but I just can't seem to figure it out. After scouring the internet for 2 days and watching "how to create a button" tutorials over and over it is still not making sense to me. I click "Calculate" and nothing happens, so I know its with the button.

var $ = function (id) {
return document.getElementById(id);
}
var calculateClick = function () {
    var billAmount = parseFloat( $("billAmount").value );
    var percentTip = parseFloat( $("rate").value );
    var amountPeople = parseInt( $("years").value );

if (isNaN(billAmount) || billAmount <= 0) {
    alert("Your bill can't be 0 or less.");
} 

else if(isNaN(percentTip) || percentTip <= 0) {
    alert("The percentage should be a whole number.");
}

else if(isNaN(amountPeople) || amountPeople <= 0) {
    alert("You are 1 person never count yourself as less.");
}

else {
    var total = billAmount * (percentTip / 100) / amountPeople;

    $("totalTip").value = total.toFixed(2);
} 
}

window.onload = function () {
$("Calculate").onclick = calculateClick;
$("billAmount").focus();
}
chinaowl

Check out this pen for a working version: http://codepen.io/anon/pen/avaRVm

First, case sensitivity, as mentioned in the comments: calculate does not equal Calculate.

The bigger problem is here:

var percentTip = parseFloat( $("rate").value );
var amountPeople = parseInt( $("years").value );

and here:

<label for="percentTip">Percent To Tip:</label>
<input type="text" id="percentTip"><br>

<label for="amountPeople">How Many People?:</label>
<input type="text" id="amountPeople"><br>

In your HTML, you don't use rate and years as IDs; you use percentTip and amountPeople. If you get those to match up, then your code will work.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Javascript If statement for simple dinner tip calculator

From Dev

Loop in Tip Calculator (Python)

From Dev

Calculator tip why is it not compiling

From Dev

Java tip calculator

From Dev

Alert box Tip Calculator

From Dev

Tip Calculator - Swift

From Dev

Making a one rep max calculator in JavaScript, not sure why my function isn't working

From Dev

Why isn't this Javascript calculator working

From Dev

Javascript GPA calculator, Calculate button not working

From Dev

Why isn't my javascript calculator working with the base.html?

From Dev

Printing a table from a tip calculator using php

From Dev

Bootstrap tool tip on label not working

From Dev

I am making a Quadratic Form Calculator, accepting a, b, and c where a isn't equal to 0

From Java

Round a tip with Javascript-Canvas

From Dev

Product price calculator isn't working

From Dev

Why isn't this Ruby calculator working as intended?

From Dev

user control tool tip not working in transparent sections

From Dev

Show() when class "tip" is found not working

From Dev

Tool tip not working on text area bootstrap

From Dev

Why is my d3.tip not working?

From Dev

css tool tip hint--top is not working

From Dev

css tool tip hint--top is not working

From Java

My mini calculator isn't working. Please read description

From Dev

javascript isn't working on html

From Dev

Javascript innerWidth isn't working

From Dev

For loop in Javascript isn't working

From Dev

Javascript innerWidth isn't working

From Dev

Javascript in PHP isn't working

From Dev

My JavaScript isn't working