Dynamic Text into HTML page using Javascript

This “Dynamic Text into HTML pages using JavaScript” explains clearly how to insert a text into a web page at runtime using logical functions available in JavaScript library.


Dynamic Text into HTML page:

  • We can place information dynamically into HTML document using Javascript.
  • Generally, HTML code is static information.
  • HTML doesn’t have the functionality to read and place information at runtime.
  • Javascript facilitates this feature.
  • A JavaScript statement like this: document.write(“<h1>” + name + “</h1>”) can write a variable text into an HTML page.

 

<html>
<body>
<h1>My First Web Page</h1>
<script type=”text/javascript”>
document.write(“<p>” + Date() + “</p>”);
</script>
</body>
</html>


<!DOCTYPE html>
<html>
<body>
<h2>Changing id information using JavaScript</h2>
<p id=”p1″>Javabook</p>
<script>
document.getElementById(“p1”).innerHTML = “Java2Python”;
</script>
<p>The paragraph information was changed by JavaScript code</p>
</body>
</html>


<!DOCTYPE html>
<html>
<body>
<h1 id=”name”>Javabook.in</h1>
<script>
var element = document.getElementById(“name”);
element.innerHTML = “Java2Python”;
</script>
<p>”Javabook” to “Java2Python”</p>
</body>
</html>


> JavaScript Events – JavaScript Tutorial < 

Share this