HTML DOM Span object

HTMLWeb DevelopmentFront End Technology

The HTML DOM span object is associated with the HTML <span> element. We can create and access span element using the createElement() and getElementById() method respectively.

Syntax

Following is the syntax for −

Creating a span object −

var a = document.createElement("SPAN");

Example

Let us look at an example for the span object −

Live Demo

<!DOCTYPE html>
<html>
<body>
<h1>Span object example</h1>
<p>Create a span element by clicking the below button</p>
<button onclick="createSpan()">CREATE</button>
<p>This is some text inside a p element</p>
<script>
   function createSpan() {
      var s = document.createElement("SPAN");
      var txt = document.createTextNode("This is some text inside the span element");
      s.appendChild(txt);
      document.body.appendChild(s);
   }
</script>
</body>
</html>

Output

This will produce the following output −

On clicking the CREATE button −

raja
Published on 19-Aug-2019 12:51:30
Advertisements