Javascript: First example explanations

This entry is part 5 of 27 in the series Javascript Tutorial

<!DOCTYPE html>
<html>
 <body>
 <h1>First JavaScript Program</h1>
 <p>Click the button to change the sentence</p>
 <p id="first">First Program</p>
 <button type="button" onclick="myFunction()">Try it</button>
 <script>
       function myFunction() {
       document.getElementById("first").innerHTML = "This is my first 
                Javascript program";
 }
 </script>
 </body>
</html>

button

In this example, we first look at this line:

<p id="first">First Program</p>

<p> is a HTML element denoting paragraph and it is using id=”first” to identify this paragraph.

To change the text “First Program” in this paragraph, let’s look at this:

 <script>
       function myFunction() {
       document.getElementById("first").innerHTML = "This is my first 
                Javascript program";
 }
 </script>

The

document.getElementById("first").innerHTML = "This is my first 
                Javascript program";

will change the text inside the paragraph to the new text.

 

Series Navigation<< Javascript: First example
Javascript: Object Oriented Javascript Program >>Javascript: Variables >>

Leave a comment

Leave a Reply