I have a piece of code that obtains 10 input values (integers and floats) from the user:
var in1 = parseInt(document.getElementById("in1").value);
var in2 = parseInt(document.getElementById("in2").value);
var in3 = parseFloat(document.getElementById("in3").value);
var in4 = parseFloat(document.getElementById("in4").value);
var in5 = parseInt(document.getElementById("in5").value);
var in6 = parseInt(document.getElementById("in6").value);
var in7 = parseInt(document.getElementById("in7").value);
var in8 = parseFloat(document.getElementById("in8").value);
var in9 = parseFloat(document.getElementById("in9").value);
var in10 = parseInt(document.getElementById("in10").value);
I then put all inputs into an array, such that they are easier to work with:
var arr = [in1, in2, in3, in4, in5, in6, in7, in8, in9, in10];
And the idea is to adjust the CSS of an HTML container by iterating through the values, and adjusting the specific container, whose value is equal to 0.
for (let i = 0; i < arr.length; i++) {
if (arr[i] === 0) {
document.getElementById(**container of the corresponding arr[i] value**).style.color = "red";
} else {
document.getElementById(**container of the corresponding arr[i] value**).style.color = "black";
}
}
Now how can I solve this?
I can get the code to run, if I just work with one container, say in1
, so if I put ("in1")
instead of the
(**container of the corresponding arr[i] value**)
4
Answers
way too complicated. Give them all the same class and select them all with
querySelectorAll
. Then useforEach
to iterate over all those elements. Use a ternary conditional operator to apply the color:EDIT: When possible follow @tacoshy ‘s answer
First I would make an array of objects with values,
parser
andelement
Where
parser
is the parser to convert the value from your HTML element (parseInt
orparseFloat
). And whereelement
is the HTML/DOM element, so you don’t have to calldocument.getElementById
too much, because it is not performant to use it every time you need it.When you want to set te color, you can loop over it by using the js
for-of
loop, where we also use the ES6 object destructuring to get theparser
and theelement
as a variable.Then we use the
parser
with the value of theelement
to parse it into a number.You could select all inputs by class and then iterate over them. adding an event listener on keyup or on change. within the event callback you can change the colour based on the conditions you wish to check. In this example the text in the input canges colour based on the length of the value.
At my opinion…
If some values are parseFloat and others parseInt, this will cause a problem if you want to add an event to update the colors when the value change.
For the parseInt values the color will stay red until you reach an int number (1 or -1 in this case) but there will be no problem with the float numbers (0.1, 0.2, 0.3, 0.4, 0,5 … 0.9) or negative (-0.1, -0.2, -0.3, -0.4, -0,5 … -0.9).
When using parseInt(), any non-integer value will be converted to 0
Here is a snippet even it’s not elegant, you’ll understand the problem when you change values.
The options in the input field
step="0.1" min="-10" max="10"
are optional.The first snippet is there just to understand the mistakes, there’s another snippet under the first one that solve the issue.
Here You will not have the same problem and the code is shorter.
This is the snippet that solve the issue in case of numbers such :
0,1 to 0,9:
The following code involves an array of objects to store the elements, their values, and their IDs, and four functions to get the HTML elements, get their values, log the objects, and style the objects based on their values.
I know that this code is more difficult to read than tacoshy’s answer but may be more flexible and can be extended to handle more elements and more complex logic. However, it may be overkill for this specific use case.
I made a lot of edits in this answer to add the comments in the
getElementsValues()
function in the first snippet and to do my best to explain what were the issues with the first code.I hope that the other explanations will help you to understand as precisely as possible the different cases and their implications.
Best regards.