The Paragraph object represents an HTML <p> element.
Property | Description |
---|---|
align | Not supported in HTML5.
Use style.textAlign instead. Sets or gets the alignment |
The Paragraph object supports the standard properties and events.
We can access a <p> element by using getElementById().
<!DOCTYPE html>
<html>
<body>
<p id="myP">I am a paragraph.</p>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!--from w w w .jav a 2 s . co m-->
var x = document.getElementById("myP").id;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
The code above is rendered as follows:
We can create a <p> element by using the document.createElement() method.
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {<!-- w ww . ja v a 2s. c o m-->
var x = document.createElement("P");
var t = document.createTextNode("This is a paragraph.");
x.appendChild(t);
document.body.appendChild(x);
}
</script>
</body>
</html>
The code above is rendered as follows: