The Section object represents an HTML <section> element.
The Section object supports the standard properties and events.
We can access a <section> element by using getElementById().
<!DOCTYPE html>
<html>
<body>
<section id="mySection">
<h1>Section title</h1>
<p>this is a test</p>
</section><!-- ww w. j ava2 s . co m-->
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
var x = document.getElementById("mySection");
x.style.color = "blue";
}
</script>
</body>
</html>
The code above is rendered as follows:
We can create a <section> element by using the document.createElement() method.
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {<!--from w w w . j a v a 2s . 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("h1");
heading.appendChild(txt1);
document.getElementById("mySection").appendChild(heading);
var para = document.createElement("P");
var txt2 = document.createTextNode("test");
para.appendChild(txt2);
document.getElementById("mySection").appendChild(para);
}
</script>
</body>
</html>
The code above is rendered as follows: