Create a Section Element - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Section

Introduction

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

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">create a SECTION element containing a H1 and P element</button>

<script>
function myFunction() {//  w  w  w .j a  v  a  2 s.c  o  m
    var x = document.createElement("SECTION");
    x.setAttribute("id", "mySection");
    document.body.appendChild(x);

    var heading = document.createElement("H1");
    var txt1 = document.createTextNode("Section Heading");
    heading.appendChild(txt1);
    document.getElementById("mySection").appendChild(heading);

    var para = document.createElement("P");
    var txt2 = document.createTextNode("Some text in section..");
    para.appendChild(txt2);
    document.getElementById("mySection").appendChild(para);
}
</script>

</body>
</html>

Related Tutorials