Javascript can’t find element by id? [duplicate]

The problem is that you are trying to access the element before it exists. You need to wait for the page to be fully loaded. A possible approach is to use the onload handler: window.onload = function () { var e = document.getElementById(“db_info”); e.innerHTML=’Found you’; }; Most common JavaScript libraries provide a DOM-ready event, though. … Read more

Javascript getElementById lookups – hash map or recursive tree traversal?

I know about the Firefox and WebKit DOM implementations, both of them use a hashtable to lookup the elements, digging into the source of them you can give a look to the internal implementations: WebKit implementation, Document.cpp, uses the hashtable if the id is unique, otherwise it traverses the document to get the first match: … Read more

It says that TypeError: document.getElementById(…) is null [duplicate]

Make sure the script is placed in the bottom of the BODY element of the document you’re trying to manipulate, not in the HEAD element or placed before any of the elements you want to “get”. It does not matter if you import the script or if it’s inline, the important thing is the placing. … Read more

getElementById in React

You need to have your function in the componentDidMount lifecycle since this is the function that is called when the DOM has loaded. Make use of refs to access the DOM element <input type=”submit” className=”nameInput” id=”name” value=”cp-dev1″ onClick={this.writeData} ref = “cpDev1″/> componentDidMount: function(){ var name = React.findDOMNode(this.refs.cpDev1).value; this.someOtherFunction(name); } See this answer for more info … Read more

JavaScript getElementByID() not working [duplicate]

At the point you are calling your function, the rest of the page has not rendered and so the element is not in existence at that point. Try calling your function on window.onload maybe. Something like this: <html> <head> <title></title> <script type=”text/javascript”> window.onload = function(){ var refButton = document.getElementById(“btnButton”); refButton.onclick = function() { alert(‘I am … Read more

Can I use document.getElementById() with multiple ids?

document.getElementById() only supports one name at a time and only returns a single node not an array of nodes. You have several different options: You could implement your own function that takes multiple ids and returns multiple elements. You could use document.querySelectorAll() that allows you to specify multiple ids in a CSS selector string . … Read more

tech