Javascript DOM HTML Paragraph Object get

Introduction

The Paragraph object represents an HTML <p> element.

We can access a <p> element via document.getElementById():

var x = document.getElementById("myP");

View in separate window

<!DOCTYPE html>
<html>
<body>
<p id="myP">I am a paragraph. Click the button to get my id.</p>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {//  w w  w  . j ava  2s .  c o m
  var x = document.getElementById("myP").id;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>



PreviousNext

Related