Untitled

 avatar
unknown
plain_text
3 years ago
1.5 kB
3
Indexable

function onFormSubmit() {
  // call all the below functions here to use this function in the form while implementing onsubmit.
   var data = readFormData();
   insertNewRecord(data);
   resetForm();
}

function readFormData() {
 // return all the input values from three input fields here.
  var iv1 = document.getElementById("fname").value;
  var iv2 = document.getElementById("lname").value;
  var iv3 = document.getElementById("location").value;
  return [iv1,iv2,iv3];
}

function insertNewRecord(data) {
// this function should insert a new row with data in the table.
var first = data[0];
var second = data[1];
var third = data[2];
var tbodyRef = document.getElementById("employeeList").getElementsByTagName('tbody')[0];
var newRow = tbodyRef.insertRow();
// Insert a cell at the end of the row
var newCell1 = newRow.insertCell(0);
var newCell2 = newRow.insertCell(1);
var newCell3 = newRow.insertCell(2);
// Append a text node to the cell
var newText1 = document.createTextNode(first);
newCell1.appendChild(newText1);
var newText2 = document.createTextNode(second);
newCell2.appendChild(newText2);
var newText3 = document.createTextNode(third);
newCell3.appendChild(newText3);
if(first===""){
  alert("input field cannot be empty");
}
}
function resetForm() {
 // this function should reset the form fields.
   document.getElementById("formvalue").reset();
}
if (typeof exports !== "undefined") {
  module.exports = {
    onFormSubmit,
    readFormData,
    insertNewRecord,
    resetForm,
  };
}
Editor is loading...