You need to get a reference of your form, and after that you can iterate the elements
collection. So, assuming for instance:
<form method="POST" action="submit.php" id="my-form">
..etc..
</form>
You will have something like:
var elements = document.getElementById("my-form").elements;
for (var i = 0, element; element = elements[i++];) {
if (element.type === "text" && element.value === "")
console.log("it's an empty textfield")
}
Notice that in browser that would support querySelectorAll you can also do something like:
var elements = document.querySelectorAll("#my-form input[type=text][value='']")
And you will have in elements
just the element that have an empty value attribute. Notice however that if the value is changed by the user, the attribute will be remain the same, so this code is only to filter by attribute not by the object's property. Of course, you can also mix the two solution:
var elements = document.querySelectorAll("#my-form input[type=text]")
for (var i = 0, element; element = elements[i++];) {
if (element.value === "")
console.log("it's an empty textfield")
}
You will basically save one check.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…