So I'm using querySelectorAll on four inputs on the page (height, weight, age, gender), and adding a change event to each.
var inputs = document.querySelectorAll('input');
input.addEventListener('change', changeValue));
If I then wanted to create a string of all of the items values, during the loop, how would I do that?
function changeValue() {
this.name === 'weight' ? this.value + 'lbs' + this.name === 'height' ? this.value + 'inches';
}
Basically, I'm not sure how I get a specific inputs value out of the loop without using something as generic as an index.
"If I then wanted to create a string of all of the items values"
You could
JSON.stringifythem. Also, one approach that I use a lot when dealing with sets in Javascript is to iterate withforEachin arrays.Something like this would do it without much complexity:
Edited:
It seems that
querySelectorAllimplementsforEachsince ES5, so there is no need on a decent browser to convertquerySelectorAllresults withArray.from.