From my perspective: mixing parseFloat and parseInt values can lead to complications when trying to update colors upon value changes.
For parseInt values, the color will remain red until an integer (1 or -1) is reached. However, float numbers (0.1, 0.2, 0.3, etc.) and negative values (-0.1, -0.2, -0.3, etc.) will not encounter this issue.
Using parseInt() on non-integer values will result in conversion to 0.
Below is a snippet that may appear inelegant but helps illustrate the problem when values are altered:
The parameters "step="0.1" min="-10" max="10"" in the input field are optional.
The initial snippet aims to highlight the errors, followed by a subsequent snippet resolving the issue.
let elements_values=[];
function getHTMLElements(){
for (var i=1; i <=10; i++){
var el = "in" + i;
elements_values.push({element:document.getElementById(el),value:0,id:i-1});
element:document.getElementById(el).addEventListener("change",dealChange);
}
}
//getElementsValues is inappropriate (refer to snippet 2)
function getElementsValues(){
elements_values[0].value = parseInt(elements_values[0].element.value);
elements_values[1].value = parseInt(elements_values[1].element.value);
elements_values[2].value = parseFloat(elements_values[2].element.value);
elements_values[3].value = parseFloat(elements_values[3].element.value);
elements_values[4].value = parseInt(elements_values[4].element.value);
elements_values[5].value = parseInt(elements_values[5].element.value);
elements_values[6].value = parseInt(elements_values[6].element.value);
elements_values[7].value = parseFloat(elements_values[7].element.value);
elements_values[8].value = parseFloat(elements_values[8].element.value);
elements_values[9].value = parseInt(elements_values[9].element.value);
}
//see the other snippet for adjustments to this function.
function logObjects(){
for (var i=0; i <10; i++){
console.log("element = " + elements_values[i].element + " value = " + elements_values[i].value + " id = " + elements_values[i].id);
}
}
function styleObject(){
for (var i = 0; i < elements_values.length; i++) {
if (elements_values[i].value === 0) {
elements_values[i].element.style.color = "red";
elements_values[i].element.style.backgroundColor = "#cccccc";
elements_values[i].element.style.fontWeight = "bold";
} else {
elements_values[i].element.style.color = "black";
elements_values[i].element.style.backgroundColor = "#eeeeee";
elements_values[i].element.style.fontWeight = "normal";
}
}
}
function dealChange(e){
getElementsValues();
styleObject();
}
getHTMLElements();
getElementsValues();
//logObjects();
styleObject();
<input id="in1" type="number" value="0.9" step="0.1" min="-10" max="10">
<input id="in2" type="number" value="2" step="0.1" min="-10" max="10">
<input id="in3" type="number" value="0.3" step="0.1" min="-10" max="10">
<input id="in4" type="number" value="0" step="0.1" min="-10" max="10">
<input id="in5" type="number" value="1.5" step="0.1" min="-10" max="10">
<input id="in6" type="number" value="6.67" step="0.1" min="-10" max="10">
<input id="in7" type="number" value="0" step="0.1" min="-10" max="10">
<input id="in8" type="number" value="8.02" step="0.1" min="-10" max="10">
<input id="in9" type="number" value="9.88" step="0.1" min="-10" max="10">
<input id="in10" type="number" value="-0.6" step="0.1" min="-10" max="10">
A more concise approach that circumvents the same issues is provided below:
This revised snippet effectively handles decimal numbers from 0.1 to 0.9:
The code entails an array of objects to store elements, their values, IDs, as well as functions to handle HTML elements, capture their values, present information about the objects, and apply styles based on values.
While this solution may be convoluted compared to tacoshy's response, it offers greater flexibility and scalability for managing additional elements and complex logic. Nonetheless, it might be excessive for this specific scenario.
let elements_values=[];
function getHTMLElements(){
for (var i=1; i <=10; i++){
var el = "in" + i;
elements_values.push({element:document.getElementById(el),value:0,id:i-1});
element:document.getElementById(el).addEventListener("change",dealChange);
}
}
function getElementsValues(){
for (var i=0; i <10; i++){
elements_values[i].value = parseFloat(elements_values[i].element.value);
}
}
function logObjects(){
for (var i=0; i <10; i++){
console.log("element = " + elements_values[i].element + " value = " + elements_values[i].value + " id = " + elements_values[i].id);
}
}
function styleObject(){
for (var i = 0; i < elements_values.length; i++) {
if (parseFloat(elements_values[i].value) === 0) {
elements_values[i].element.className = "equalZero";
} else {
elements_values[i].element.className = "notEqualZero";
}
}
}
function dealChange(e){
getElementsValues();
styleObject();
}
getHTMLElements();
getElementsValues();
//logObjects();
styleObject();
.equalZero {
color: red;
background-color: #cccccc;
font-weight: bold;
}
.notEqualZero {
color: black;
background-color: #eeeeee;
font-weight: normal;
}
<input id="in1" type="number" value="0.9" step="0.1" min="-10" max="10">
<input id="in2" type="number" value="2" step="0.1" min="-10" max="10">
<input id="in3" type="number" value="0.3" step="0.1" min="-10" max="10">
<input id="in4" type="number" value="0" step="0.1" min="-10" max="10">
<input id="in5" type="number" value="1.5" step="0.1" min="-10" max="10">
<input id="in6" type="number" value="6.67" step="0.1" min="-10" max="10">
<input id="in7" type="number" value="0" step="0.1" min="-10" max="10">
<input id="in8" type="number" value="8.02" step="0.1" min="-10" max="10">
<input id="in9" type="number" value="9.88" step="0.1" min="-10" max="10">
<input id="in10" type="number" value="-0.6" step="0.1" min="-10" max="10">
In refining this answer, comments were added to the getElementsValues()
function within the first snippet to elucidate the issues with the initial code.