Getting an HTML H1 value to JavaScript variable?

JavascriptWeb DevelopmentObject Oriented Programming

To get the value of H1 to JavaScript variable, you can use −


Let’s say the following is our H1 heading −

<h1 id="demo"> This is the demo program of JavaScript ........</h1>

Now, let’s get the H1 value using the below code −


 Live Demo

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initialscale=1.0">
<link rel="stylesheet"href="//">
<script src=""></script>
<script src=""></script>
<h1 id="demo"> This is the demo program of JavaScript ........</h1>
   var data=document.getElementById('demo').innerHTML;
   console.log("The data is="+data);

To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.


This will produce the following output −

Updated on 03-Sep-2020 06:25:33