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