LearnFast.Ninja

JavaScript write html to div with text string using innerHTML

Sample of how to write html to div. On body write some div with id attribute. On your script write the following java script. You can copy then paste the working example.

Step 1: On body write some div with id attribute.
<div id="MyDiv">
 
</div>

Step 2: On your script write the following JavaScript. 
document.getElementById("MyDiv").innerHTML = "<b>I am bold item</b>";

Working example:

<html>

<head>

 <title>Write to div</title>

 <script type="text/javascript">

     function Write_To_Div() {

         document.getElementById("MyDiv").innerHTML = "<b>I am bold item</b>";

     }

 </script>

</head>

<body onload="Write_To_Div()">

 <div id="MyDiv">

 

 </div>

</body>

</html>



Tags

javascript

getelementbyid innerhtml div