How to getElementByClass instead of GetElementById with JavaScript?

The getElementsByClassName method is now natively supported by the most recent versions of Firefox, Safari, Chrome, IE and Opera, you could make a function to check if a native implementation is available, otherwise use the Dustin Diaz method: function getElementsByClassName(node,classname) { if (node.getElementsByClassName) { // use native implementation if available return node.getElementsByClassName(classname); } else { … Read more

Get element inside element by class and ID – JavaScript

Well, first you need to select the elements with a function like getElementById. var targetDiv = document.getElementById(“foo”).getElementsByClassName(“bar”)[0]; getElementById only returns one node, but getElementsByClassName returns a node list. Since there is only one element with that class name (as far as I can tell), you can just get the first one (that’s what the [0] … Read more

Getting the parent div of element

You’re looking for parentNode, which Element inherits from Node: parentDiv = pDoc.parentNode; Handy References: DOM2 Core specification – well-supported by all major browsers DOM2 HTML specification – bindings between the DOM and HTML DOM3 Core specification – some updates, not all supported by all major browsers HTML5 specification – which now has the DOM/HTML bindings … Read more

How to access a DOM element in React? What is the equilvalent of document.getElementById() in React

You can do that by specifying the ref EDIT: In react v16.8.0 with function component, you can define a ref with useRef. Note that when you specify a ref on a function component, you need to use React.forwardRef on it to forward the ref to the DOM element of use useImperativeHandle to to expose certain … Read more

Do DOM tree elements with IDs become global properties?

What is supposed to happen is that ‘named elements’ are added as apparent properties of the document object. This is a really bad idea, as it allows element names to clash with real properties of document. IE made the situation worse by also adding named elements as properties of the window object. This is doubly … Read more