Javascript DOM HTML BR Object create

Introduction

The BR object represents an HTML <br> element.

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

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

Click the button to create some BR elements.

View in separate window

<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Test</button>
<div id="myDIV">
  <span>Hello,</span>
  <span>CSS.</span>
  <span>HTML.</span>
  <span>Javascript.</span>
</div>/*  w w  w . j a v  a 2 s .com*/

<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>



PreviousNext

Related