Step 1 – Create HTML5 document
First and the most important step is to create a HTML5 document in your code editor. I use Visual Studio Code, but you can choose your own which you like to use. Below is the screenshot of the document which I created below.
Step 2 – Create a div with ID
Now we successfully created a blank HTML5 document with basic things. Now let’s create a div with some specific Id. Here I have created a div with id of “myDiv”. And below is the screenshot of my code.
script tag at the end of the body, but wrapped into body tag. And add the following code in your document
let myVar = 'Some Variable';
let myDiv = document.getElementById('myDiv');
myDiv.innerHTML = myVar;
Below is the screenshot of the final code and I shall describe every line below.
On line #11 and #15 are the
On Line #12 we have declared a variable with
myVar name and assigned ‘some variable value’ to it.
Second last on line #13 we have got the div by it’s Id and saved into
Lastly on line #14 we have displayed our variable by using innerHTML DOM property.