I know "getElementsByClassName
" is not support by IE8
. Do you know what can I use instead? I am getting annoying by error
"Object doesn't support this property or method".
The HTML code is:
function sumar() { var elems = document.getElementsByClassName('verdana14 toAdd'); var myLength = elems.length; total = 0; for (var i = 0; i < myLength; ++i) { if (elems[i].value!="") { total += parseInt(elems[i].value,10); } } var promedio = total/myLength; parseFloat(document.getElementById('promediocal').value = promedio.toFixed(2)); }
This the input text that calls the javascript function:
<input name='AE_EA_1_BIV_003_2' type='text' class='verdana14 toAdd' id='AE_EA_1_BIV_003_2' style='width:50px' onChange='sumar()'/> <input name='AE_EA_1_BIV_003_3' type='text' class='verdana14 toAdd' id='AE_EA_1_BIV_003_3' style='width:50px' onChange='sumar()'/> <input name='AE_EA_1_BIV_003_4' type='text' class='verdana14 toAdd' id='AE_EA_1_BIV_003_4' style='width:50px' onChange='sumar()'/>
The getElementsByClassName method of Document interface returns an array-like object of all child elements which have all of the given class name(s). When called on the document object, the complete document is searched, including the root node.
getElementById() is used to access the DOM elements using their id and getElementsByClassName() is used to access element(s) using their class .
getElementsByClassName('link') // Output : will return all the element with a class "link" but whereas in query selector it will return only one element which encounters first.
To find all elements by className in React: Use the getElementsByClassName method to get all elements with a specific class. Place your call to the method in the useEffect() hook.
Use document.querySelectorAll('.verdana14.toAdd')
.
See also my related blog post.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With