The Span object represents an HTML <span> element.
The Span object supports the standard properties and events.
The following code shows how to get <span> element by using getElementById()
<!DOCTYPE html>
<html>
<body>
<span id="mySpan" style="color:blue;">blue</span>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!--from w w w.jav a 2 s. c o m-->
var x = document.getElementById("mySpan").style.color;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
The code above is rendered as follows:
The following code shows how to create a <span> element by using the document.createElement() method
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {<!--from w ww. j a v a 2 s. com-->
var x = document.createElement("SPAN");
var t = document.createTextNode("span");
x.appendChild(t);
document.body.appendChild(x);
}
</script>
</body>
</html>
The code above is rendered as follows: