I’m trying to querySelectorAll a bunch of things in my HTML document.
I then want to create a variable for each of them.
Individually, I would do the following to establish it, and then update it.
const workersOutput = document.getElementById('workersOutput');
workersOutput.textContent = `${workers}`;
This is really messy though when I’ll have many of these to do.
Using querySelectorAll, I can make some hybrid version, but it’s not exactly readable or easy to use.
const outputs = document.querySelectorAll('.output');
outputs[2].textContent = `${workers}`;
Couldn’t I use maybe forEach to create a variable using the ID or something along the lines of (I know that’s not how that works, but I want to get the idea across:
const outputs = document.querySelectorAll('.output');
outputs.forEach((output) => {
const outputs[2] = document.getElementById(output.id);
});
I could also be way off on how to accomplish this the “right way”, I’m newish to coding.