Edit This Code:
<!DOCTYPE html>
<html>
<body>

<p>Click the button to create some BR elements.</p>

<button onclick="myFunction()">Try it</button>

<p>When you click the button, a br element is inserted before each span element in div, causing an appropriate line break in the text below.</p>

<div id="myDIV">
  <span>Hello,</span>
  <span>My name is Bob.</span>
  <span>I live in Bobville.</span>
  <span>I am 45 years old.</span>
</div>

<script>
function myFunction() {
    // Get the div element with id="myDIV"
    var div = document.getElementById("myDIV");

    // Get all span elements inside of div
    var spans = div.getElementsByTagName("SPAN");  

    // Create a loop which will insert a br element before each span element in div, starting from the second span element
    var i;
    for (i = 1; i < spans.length; i++) {
        var newElem = document.createElement("BR");
        div.insertBefore(newElem, spans[i]);
    }
}
</script>

</body>
</html>


Result:
Try it Yourself - © w3schools.com
Privacy Policy