I have a page that contains a whole bunch of input boxes of type number. These can be individually changed, or a set of them can be changed by a selector.
I would like to show the user which input boxes have values greater than 0 in them by changing the class of the input box.
I cannot use JQuery. It has to be done with pure Javascript.
I understand I will need to use an event listener to attach a function to it?
Anyway... here's a piece of html as an example:
<input name="qty1" type="number" min="0" value="0">
<input name="qty2" type="number" min="0" value="0">
<input name="qty3" type="number" min="0" value="0">
<input name="qty4" type="number" min="0" value="0">
<input name="qty5" type="number" min="0" value="0">
Here's some Javascript I think should go with this:
function changeInputClass(element){
if(element.value > 0){
//add class 'bold-text' to this element
}
else {
//remove class 'bold-text' from this element
}
}
var myElements = document.getElementsByTagName('input');
for(var i=0; i<myElements.length; i++){
if(myElements[i].type == 'number') {
// add event listener here to call function changeInputClass()
}
}
Appreciate the help.
Update of more realistic representation of the html:
<div id="selections" class="selections">
<div class="images-container">
<div>
<div class="image-options">
<div class="product-option">
<div class="qty"><input name="qty1" type="number" min="0" value="0"></div>
<div>Product Description 1</div>
</div>
<div class="product-option">
<div class="qty"><input name="qty2" type="number" min="0" value="0"></div>
<div >Product Description 2</div>
</div>
<div class="product-option">
<div class="qty"><input name="qty3" type="number" min="0" value="0"></div>
<div >Product Description 3</div>
</div>
</div>
<div class="image-options">
<div class="product-option">
<div class="qty"><input name="qty4" type="number" min="0" value="0"></div>
<div >Product Description 4</div>
</div>
<div class="product-option">
<div class="qty"><input name="qty5" type="number" min="0" value="0"></div>
<div >Product Description 5</div>
</div>
<div class="product-option">
<div class="qty"><input name="qty6" type="number" min="0" value="0"></div>
<div >Product Description 6</div>
</div>
</div>
</div>
</div>
</div>
Created a fiddle http://jsfiddle.net/narfie/g3gbqaxz/14/