Create a BR Element - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:BR

Introduction

You can create a <br> element by using the document.createElement() method:

In the following code, a br element is inserted before each span element in div.

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">create some BR elements</button>

<div id="myDIV">
  <span>A</span>
  <span>B</span>
  <span>C</span>
  <span>D</span>
</div>//from   www  .  jav a 2s  .co m

<script>
function myFunction() {
    var div = document.getElementById("myDIV"); 

    var spans = div.getElementsByTagName("SPAN");  

    var i;
    for (i = 1; i < spans.length; i++) {
        var newElem = document.createElement("BR");
        div.insertBefore(newElem, spans[i]);
    }
}
</script>

</body>
</html>

Related Tutorials