Cannot set property 'innerHTML' of null
You have to place the hello
div before the script, so that it exists when the script is loaded.
Cannot set property innerHTML of null
If You use 'getElementById', those "Id" refer to HTML Id. So, in Your HTML u should have element with id="testCurrency".
for example:
<body>
<button onclick="addNumber(1)">
<p> id="testCurrency"></p>
<script type="text/javascript" src="main.js"></script>
</body>
How can I solve error Cannot set property 'innerHTML' of null
You need to close your tags. Your code can't see 'div2' because 'div' is never getting closed.
<div id='div'></div>
<div id='div2'></div>
Then, in your again
function you're calling document.write
which shouldn't be used after page load because it writes over anything you already had on the page. It is effectively removing your <div>
tags, which is then causing an error when you're trying to access them. Plus, you're already putting that value on the page through innerHTML
. Short story: remove the document.write
line in the button
function Given error Uncaught TypeError: Cannot set property 'innerHTML' of null when button is clicked
The error
Uncaught TypeError: Cannot set property 'innerHTML' of nullsuggests that it can't set the
innerHTML
property of an a null
value. A null
value is the result of a getElementById()
query which comes up empty.So looking at what line the error occurs you can see it can't set the innerHTML
of theresult9h
element. And that is because it does not exist in your HTML.
ERROR TypeError: Cannot set property 'innerHTML' of null when trying to alter InnerHtml property of a label (Typescript / HTML / Angular)
The syntax for getElementById is wrong, you only pass in the name of the id. That is why element was null.
const element: HTMLElement = document.getElementById('file') as HTMLElement;
Also, use it is a good practice to check if element exist or not , so it can be something like if(element)
element.innerHTML = fileName;
Stackblitz
Related Topics
Problems with Circular Dependency and Oop in Angularjs
Understanding JavaScript Immutable Variable
Remove HTML Comments with Regex, in JavaScript
Accessing JSON Object Keys Having Spaces
How to Use Local Storage in Angular
JavaScript Mapping Touch Events to Mouse Events
JavaScript String and Number Conversion
In Angular, How to Determine the Active Route
React 18, Useeffect Is Getting Called Two Times on Mount
How to Stop Intense JavaScript Loop from Freezing the Browser
Get Selected HTML in Browser via JavaScript
Javascript: Dynamically Creating Variables for Loops
How to Sign into Google with Selenium Automation Because of "This Browser or App May Not Be Secure."
External Resource Not Being Loaded by Angularjs
Basic Ajax Send/Receive with Node.Js
What Exactly Can Cause an "Hierarchy_Request_Err: Dom Exception 3"-Error
How to Change the Pop-Up Position of the Jquery Datepicker Control