"use strict"; /*----- signature: allFilled2: void -> Boolean purpose: expects nothing, returns true if the three textfields in three-value-form2.php have values and the 2nd textfield has a numeric value; otherwise, it returns false AND complains in a NEWLY-ADDED errors paragraph by: Sharon Tuttle last modified: 2016-04-15 -----*/ function allFilled2() { // get the DOM object for the document's body element // (yes, there's JUST one body element! BUT // getElementsByTagName returns an array...) var bodyObjectArray = document.getElementsByTagName("body"); var bodyObject = bodyObjectArray[0]; // get the text fields var value1Field = document.getElementById("value1"); var value2Field = document.getElementById("value2"); var value3Field = document.getElementById("value3"); // these are all input elements of type "text" -- // they have a value attribute representing // what is in that element at that point var val1 = value1Field.value; var val2 = value2Field.value; var val3 = value3Field.value; var result = true; // create an errors paragraph unless one exists already var errorsPara = document.getElementById("errors"); // IF above returned anything (and not null), // (if this p element currently exists) -- // I hope it will be "truthy" here if (errorsPara) { // since it already exists, clear the previous error message errorsPara.innerHTML = ""; // and REMOVE it from the page, until we see // if we still need it bodyObject.removeChild(errorsPara); } // if we get here, it doesn't exist yet -- // SHOULD create it, just in case else { errorsPara = document.createElement("p"); // I have a CSS rule to style the element with // id="errors", which I would like to use // for this paragraph errorsPara.id = "errors"; } // complain and return false if any fields are empty if ( (! val1.trim() ) || (! val2.trim() ) || (! val3.trim() ) ) { errorsPara.innerHTML = "MUST fill in ALL " + "textfields before submit! (white space " + "won't do)"; result = false; } // if all fields are filled (with something non-white- // space), then make sure 2nd field is numeric // some JavaScript functions: // isNaN - returns true if its argument is // not a number // parseFloat - expects a string, tries to // return the corresponding floating-point // number else if (isNaN(parseFloat(val2)) || (parseFloat(val2) != val2)) { errorsPara.innerHTML = "2nd textfield MUST contain " + "ONLY a number!"; result = false; } // only going to add the errors paragraph if result is false if (result === false) { // get the DOM object for the form var formObject = document.getElementById("valueForm"); // add errors paragraph before the form as // a new child of body bodyObject.insertBefore(errorsPara, formObject); } return result; } /*---- function: doSomething: void -> void purpose: expects nothing, returns nothing, BUT changes the value attribute of the element with id="value1" to CHANGED ya!! ----*/ function doSomething() { var desiredElement = document.getElementById("value1"); desiredElement.value = "CHANGED ya!!"; }