how to get an element by class name in javascript

index.tsx
var elems = document.getElementsByClassName("class-name");
59 chars
2 lines

This returns an array-like object of all elements with the specified class name. You can then access individual elements by their index in the array. For example:

index.tsx
var firstElem = document.getElementsByClassName("class-name")[0];
66 chars
2 lines

This assigns the first element with the specified class name to the variable firstElem.

gistlibby LogSnag