-2

There is something wrong with this code.

HTML

<p id = "Krishna"></p>

JavaScript

document.getElementById("Krishna").innerHTML = "Hello"
Tushar
  • 78,625
  • 15
  • 134
  • 154
  • Possible Duplicate of [Why does jQuery or a DOM method such as getElementById not find the element?](http://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element) – Tushar Apr 29 '16 at 13:32
  • 1
    When do you execute that js? Are you doing it before the DOM is ready? – Patrick Evans Apr 29 '16 at 13:32
  • Works fine: https://jsfiddle.net/exsspjx8/ Do you have any code that actually demonstrates the problem? – David Apr 29 '16 at 13:33

1 Answers1

1

You are not calling your script in the code above. I use the same code with a function:

<script>
function fun(){ 
 document.getElementById("Krishna").innerHTML = "Hello";
}
</script>
</head>
<body onload='fun()'>
<p id = "Krishna"></p>

</body>

I hope it works for you.

hat
  • 715
  • 15
  • 21