-1

I invoke a function named Func() in my JS code and it should change a html tag content with innerHTML method but it doesn't work.

HTML code

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="js.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>

    <p id="result">results will be here</p>

</body>
</html>

JavaScript code

function Func(){
    var a = "Hello world";
    document.getElementById("result").innerHTML = a;
}

Func();

The console shows this error:
Uncaught TypeError: Cannot set property 'innerHTML' of null
Mahdi
  • 1

1 Answers1

-1

Change so:

Document.addEventListener("DOMContentLoaded",        
function(event) { 
  Func();
});

Or if you have jquery use it for ready event.

You can call this function Func only if the Dom is ready and the element result is ready

fappiu
  • 21
  • 4