I was just trying to use document methods like getElementsByClassName
, getElementsByTagName
, getElementById
, and consoling the output. However strange thing is that only getElementById
is not working, it logs null
. I have tried className with App and TagName h1, works perfectly. Can anyone shed light on this?
This is code sandbox [ https://codesandbox.io/s/813mnx1vq2 ].
Below is my code of App
which I am rendering,
function App() {
{ console.log(document.getElementById('heading1')) }
/*
{ console.log(document.getElementsByTagName("h1")) }
// Output: HtmlCollection array which contains element with id=heading1
*/
/*
{ console.log(document.getElementsByClassName("App")) }
// Output: HtmlCollection array which contains div.App element
*/
return (
<div className="App">
<h1 id="heading1">Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
PS Edit: When is NodeList live and when is it static? . Thanks to Jonas.