set value of html input elements by class with javascript in browser?

Viewed 20138

I need set value of a bunch of input boxes based on class (class="Qty")

this works if I use the ElementID

document.getElementById('G_Qty_0').value='101'

but this doesn't work

document.getElementsByClassName('Qty').value='100'

What gives?

Cheers!

2 Answers

Since this question comes up among the first results on Google and I found a shorter solution, I'm writing it here for the benefit of those looking for a shorter code:

document.querySelectorAll('.Qty').forEach( (x) => { x.value = '100' } )
Related