HTML | DOM BR Object

The DOM BR Object is used to represent the HTML <br> element. The br element is accessed by getElementById().

Syntax:

getElementById(id)

Where “id” is the ID assigned to the br tag.



Examples-1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>br Tag</title>
    <style>
        body {
            text-align: center;
        }
          
        h1 {
            color: green;
        }
    </style>
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
    <h2>DOM br Object</h2>
  
    <!-- br tag is used here -->
    <p>GeeksforGeeks:
        <br id="GFG"> Computer science portal</p>
    <button onclick="myGeeks()">Subnit</button>
    <script>
        function myGeeks() {
            var w = document.getElementById("GFG");
            w.style.display = "none";
        }
    </script>
</body>
  
</html>

chevron_right


Output:

Before Clicking on Button :

After Clicking On Button:

Example-2: Br Object can be created by using the document.createElement Method.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
    <center>
        <h1>GeeksForGeeks</h1>
        <h2>DOM BR Object</h2>
        <button onclick="Geeks()" style="margin-bottom:20px;">
          Submit
        </button>
  
        <div id="GFG">
            <span>Hello, </span>
            <span>Geeks.</span>
            <span>For</span>
            <span>Geeks</span>
        </div>
  
        <script>
            function Geeks() {
                <!-- Get the div element with id="GFG" -->
                var g = document.getElementById("GFG");
  
                <!-- Get all span elements inside of div. -->
                var f = g.getElementsByTagName("SPAN");
  
                <!-- Create a loop which will insert a br
                     element before each span element in div, 
                     starting from the second span element. -->
                         
                var j;
                for (j = 1; j < f.length; j++) {
                    var w = document.createElement("BR");
                    g.insertBefore(w, f[j]);
                }
            }
        </script>
  </center>
  
</body>
  
</html>

chevron_right


Output:

Before Clicking on Button:

After Clicking on Button:

Supported Browsers: The browser supported by DOM Br Object are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.