Our next step is to add an output area to the form, which getFederal will update. We'll also add a select box for choosing a state for withholding.
We've added an empty state select box, which gets filled by a call to loadStates in Javascript. We've also started a section for output, adding the out_federal input box to display results.
We don't do anything with the state value yet, but we'll use it in the next lesson.
document.addEventListener("DOMContentLoaded", function() { loadStates(); }); function compute() { var earnings = getFloat('earnings'); var filingstatus = getInt('filingstatus'); var payperiods = getInt('payperiods'); getFederal(earnings, filingstatus, payperiods); } function getFederal(earnings, filingstatus, payperiods) { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4) { let res = JSON.parse(this.responseText); if (res.status == "ok") { var out_federal = document.getElementById('out_federal'); out_federal.value = res.value.toFixed(2); } } }; var uri = "http://localhost:8000/taxamount/federal income tax" + "?earnings=" + earnings.toString() + "&filingstatus=" + filingstatus.toString() + "&payperiods=" + payperiods.toString(); xhttp.open("GET", uri, true); xhttp.send(); } function loadStates() { var select = document.getElementById('state'); var states = [["AL", "Alabama"], ["AK", "Alaska"], ["AZ", "Arizona"], ["AR", "Arkansas"], ["CA", "California"], ["CO", "Colorado"], ["CT", "Connecticut"], ["DE", "Delaware"], ["FL", "Florida"], ["GA", "Georgia"], ["HI", "Hawaii"], ["ID", "Idaho"], ["IL", "Illinois"], ["IN", "Indiana"], ["IA", "Iowa"], ["KS", "Kansas"], ["KY", "Kentucky"], ["LA", "Louisiana"], ["ME", "Maine"], ["MD", "Maryland"], ["MA", "Massachusetts"], ["MI", "Michigan"], ["MN", "Minnesota"], ["MS", "Mississippi"], ["MO", "Missouri"], ["MT", "Montana"], ["NE", "Nebraska"], ["NV", "Nevada"], ["NH", "New Hampshire"], ["NJ", "New Jersey"], ["NM", "New Mexico"], ["NY", "New York"], ["NC", "North Carolina"], ["ND", "North Dakota"], ["OH", "Ohio"], ["OK", "Oklahoma"], ["OR", "Oregon"], ["PA", "Pennsylvania"], ["RI", "Rhode Island"], ["SC", "South Carolina"], ["SD", "South Dakota"], ["TN", "Tennessee"], ["TX", "Texas"], ["UT", "Utah"], ["VT", "Vermont"], ["VA", "Virginia"], ["WA", "Washington"], ["WV", "West Virginia"], ["WI", "Wisconsin"], ["WY", "Wyoming"]]; states.forEach(function(state) { var option = document.createElement('option'); option.setAttribute('value', state[0]); option.appendChild(document.createTextNode(state[1])); select.appendChild(option); }); } function getFloat(id) { var elem = document.getElementById(id).value; var f = parseFloat(elem); if (isNaN(f)) { return 0.00; } else { return f; } } function getInt(id) { var elem = document.getElementById(id).value; var f = parseInt(elem); if (Number.isInteger(f)) { return f; } else { return 0; } }
document.addEventListener("DOMContentLoaded", function() {
loadStates();
});
function compute() {
var earnings = getFloat('earnings');
var filingstatus = getInt('filingstatus');
var payperiods = getInt('payperiods');
getFederal(earnings, filingstatus, payperiods);
}
function getFederal(earnings, filingstatus, payperiods) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
let res = JSON.parse(this.responseText);
if (res.status == "ok") {
var out_federal = document.getElementById('out_federal');
out_federal.value = res.value.toFixed(2);
};
var uri = "http://localhost:8000/taxamount/federal income tax"
+ "?earnings=" + earnings.toString()
+ "&filingstatus=" + filingstatus.toString()
+ "&payperiods=" + payperiods.toString();
xhttp.open("GET", uri, true);
xhttp.send();
function loadStates() {
var select = document.getElementById('state');
var states = [["AL", "Alabama"], ["AK", "Alaska"], ["AZ", "Arizona"],
["AR", "Arkansas"], ["CA", "California"], ["CO", "Colorado"],
["CT", "Connecticut"], ["DE", "Delaware"], ["FL", "Florida"],
["GA", "Georgia"], ["HI", "Hawaii"], ["ID", "Idaho"],
["IL", "Illinois"], ["IN", "Indiana"], ["IA", "Iowa"],
["KS", "Kansas"], ["KY", "Kentucky"], ["LA", "Louisiana"],
["ME", "Maine"], ["MD", "Maryland"], ["MA", "Massachusetts"],
["MI", "Michigan"], ["MN", "Minnesota"], ["MS", "Mississippi"],
["MO", "Missouri"], ["MT", "Montana"], ["NE", "Nebraska"],
["NV", "Nevada"], ["NH", "New Hampshire"], ["NJ", "New Jersey"],
["NM", "New Mexico"], ["NY", "New York"], ["NC", "North Carolina"],
["ND", "North Dakota"], ["OH", "Ohio"], ["OK", "Oklahoma"],
["OR", "Oregon"], ["PA", "Pennsylvania"], ["RI", "Rhode Island"],
["SC", "South Carolina"], ["SD", "South Dakota"],
["TN", "Tennessee"], ["TX", "Texas"], ["UT", "Utah"],
["VT", "Vermont"], ["VA", "Virginia"], ["WA", "Washington"],
["WV", "West Virginia"], ["WI", "Wisconsin"], ["WY", "Wyoming"]];
states.forEach(function(state) {
var option = document.createElement('option');
option.setAttribute('value', state[0]);
option.appendChild(document.createTextNode(state[1]));
select.appendChild(option);
function getFloat(id) {
var elem = document.getElementById(id).value;
var f = parseFloat(elem);
if (isNaN(f)) {
return 0.00;
} else {
return f;
function getInt(id) {
var f = parseInt(elem);
if (Number.isInteger(f)) {
return 0;
Here we've changed both the compute and getFederal functions, so that they set values in the Output section.
We've also added loadStates to populate the state select box, calling it when the page loads.
Click 'Compute' below to update the results in the 'Output' section below.