I have a sports betting calculator, this has several input fields, as soon as all fields are filled out, the calculated table should be displayed below.
When the table is displayed, it should be possible to change the input fields; the table should then also change in real time.
HTML: <input type="number" class ="form-control input-lg" id="quoteBack">
JS:document.getElementById("quoteBack").addEventListener("input", function() {});
I am currently using the addEventListener "input" variant, this can only be used on one field.
But I want to check that all fields are filled out, then the table should appear
Hmm, first of all, I think you should give the input field a unique className
, then we just gonna check the fields that have that className
.
For example:
<input type="number" class ="form-control input-lg validated-field" id="quoteBack">
I gave it a className called
validated-field
.
Then let's check those fields using JavaScript:
// Get fields by className
var fields = document.getElementsByClassName("validated-field")
// Array.from will convert the HTMLCollection to Native Array.
if (Array.from(fields).length > 0) {
// Loop
Array.from(fields).forEach(field => {
// Add Event for each field.
field.addEventListener("input", function(e) {
console.log(e.target.value)
if (!e.target.value) {
e.target.classList.add("not-valid")
} else {
e.target.classList.remove("not-valid")
}
})
})
}
I just loop through the fields and add an event listener for each field.
Live Example: https://codepen.io/nawafscript/pen/OJjEXrW