Create <span> element, change its color and append to document - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Span

Description

Create <span> element, change its color and append to document

Demo Code

ResultView the demo in separate window

<html>
   <head></head>
   <body>
      ... //from   ww w.  jav  a  2  s.c  om
      ... 
      <script type="text/javascript">
        var mySpan = document.createElement("span");
        mySpan.innerHTML = "This is my span!";
        mySpan.style.color = "red";
        document.body.appendChild(mySpan);
        console.log("hi");
    
      </script> 
   </body>
</html>

Related Tutorials