Question: Create a sales _ tax.html code for this Lab This is our first JavaScript application. Open the files for the Sales Tax application 1 .
Create a salestax.html code for this Lab
This is our first JavaScript application.
Open the files for the Sales Tax application
Download the attached zip file for the lab.
Start your browser, and open the page salestax.html:
Enter valid numeric values in the first two input boxes and click the Calculate
button. Notice that nothing happens.
Use your Visual Studio Code to open the salextax.html Open the
salestax.js file for this application from there. Then, notice that this file only
contains the $ function that's used to get the object for an HTML element.
Open the salestax.html file for this application. Then, note the ids that are
used for the text boxes for this application.
Start the event handlers for this application
Start the event handler for the window.onload event by entering this code:
window.onload function
alert This is the window.onload event handler.";
Then, save the file, switch to browser, click the Reload button, and note the
message that's displayed. Or if nothing happens, check your code, fix it and
try this again.
Between the $ function and the window.onload event handler, start a
function named calculateclick by entering this code:
var calculateclick function
alert This is the calculateclick event handler.";
Then, save the code, switch to Firefox, click the Reload button, and note that
this function isn't executed. That's because it hasn't been called. Now, switch
to your editor.
Call the calculateclick function from the window.onload event handler by
entering this statement after the alert statement:
calculateclick;
Then, save the file, switch to browser, and click the Reload button. This time,
two message boxes should be displayed: one by the window.onload event
handler and one by the calculateclick function. If this doesn't work, fix the
code and try it again. Then, switch to your editor.
Assign the calculateclick function to the click event of the Calculate button
by replacing the statement you entered in step with this statement:
$calculateonclick calculateclick;
Then, save the file, switch to browser, click the Reload button, and click on
the Calculate button. If you did everything right, the two message boxes are
again displayed. Otherwise, fix the code and try again. Then, switch to your
editor.
Finish the calculateclick event handler
After you do each of the steps that follow, switch to Firefox, click the Reload button,
and make sure your changes work. Otherwise, fix your code and try again.
Add these statements after the alert statement in the calculateclick
function:
var subtotal ;
var taxRate ;
var salesTax subtotal taxRate ;
var total subtotal salesTax;
alert "subtotal subtotal
"taxRate taxRate
"salesTax salesTax
"total total;
Then, experiment with any statements that you don't understand in the
calculateclick function. To display the results of your statements, you can
use alert statements as in step
Replace the statements that you entered in step with these statements:
var subtotal parseFloat $subtotalvalue ;
var taxRate parseFloat $taxRatevalue ;
calculate results
var salesTax subtotal taxRate ;
salesTax parseFloat salesTax.toFixed;
var total subtotal salesTax;
display results
$salesTaxvalue salesTax;
$totalvalue total.toFixed;
The first two statements will get the user entries; the last two statements will
display the results. At this point, the application should work if the user enters
valid numbers.
Comment out the two alert statements by entering slashes before them.
Check the user entries for validity by entering this code with the statements
that do the calculations in the else clause:
if isNaNsubtotal isNaNtaxRate
alertPlease check your entries for validity.";
else
the statements that do the calculations should be here
This just checks to make sure that both user entries are numeric before the
calculations are done.
At this point, you have a working application. Further, you can check to make
sure that each user entry is both numeric and greater than zero, and you can
display a different error message for each text box. Or you can add a
statement to the window.onload function that moves the focus to the first
text box. When you're through experimenting, close the files
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
