Javascript DOM HTML Span Object create

Introduction

We can create a <span> element via the document.createElement() method:

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

Click the button to create a SPAN element.

View in separate window

<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Test</button>

<script>
function myFunction() {/*from  w w  w  . j a  va  2s . com*/
  var x = document.createElement("SPAN");
  var t = document.createTextNode("This is a span element.");
  x.appendChild(t);
  document.body.appendChild(x);
}
</script>

</body>
</html>



PreviousNext

Related