How to get all elements by class name on pure javascript ? Analog $('.class') in Jquery ?
The querySelectorAll() method returns all elements within the document having the same class. To get the first element that matches the specified selector, you can use the querySelector() method.
getElementsByClassName() 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.
To find all elements by className in React: Use the getElementsByClassName method to get all elements with a specific class.
The HTML class attribute is used to specify a class for an HTML element. Multiple HTML elements can share the same class.
A Simple and an easy way
var cusid_ele = document.getElementsByClassName('custid'); for (var i = 0; i < cusid_ele.length; ++i) { var item = cusid_ele[i]; item.innerHTML = 'this is value'; }
document.getElementsByClassName(klass)
Be aware that some engines (particularly the older browsers) don't have it. You might consider using a shim, if that's the case. It will be slow, and iterate over the whole document, but it will work.
EDIT several years later: You can get the same result using document.querySelectorAll('.klass')
, which doesn't seem like much, but the latter allows queries on any CSS selector, which makes it much more flexible, in case "get all elements by class name" is just a step in what you are really trying to do, and is the vanilla JS answer to jQuery's $('.class')
.
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