Javascript Reference - HTML DOM Paragraph Object








The Paragraph object represents an HTML <p> element.

Paragraph Object Properties

Property Description
align Not supported in HTML5. Use style.textAlign instead.
Sets or gets the alignment

Standard Properties and Events

The Paragraph object supports the standard properties and events.

Example

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:





Example 2

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: