Javascript DOM HTML Span Object get

Introduction

The Span object represents an HTML <span> element.

We can access a <span> element via document.getElementById():

var x = document.getElementById("mySpan");

Click the button to get the color of the span element.

View in separate window

<!DOCTYPE html>
<html>
<body>
<p>This is a <span id="mySpan" style="color:blue;">small</span> test.</p>
<button onclick="myFunction()">Test</button>
<p id="demo"></p>

<script>
function myFunction() {//  w w w  . j  a  v a  2 s .com
  var x = document.getElementById("mySpan").style.color;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>



PreviousNext

Related