-3

I have 2 html files. I want to take the innerHTML of an element of the 1st HTML file and display it in the 2nd HTML.

This is the 1.html file

<h1 id="data">DATA</h1>
<button onclick="go()">GO</button>

This is the 2.html file

<h1 id="display"></h1>

This is what I tried in js but didn't work

function go() {
    let data = document.getElementById("data").innerHTML;
    document.getElementById("display").innerHTML = data;
    window.open("2.html")
}

Please someone help

Luis Estevez
  • 1,387
  • 12
  • 19
  • 1
    `document.getElementById("display")` will not work until `2.html` is loaded in the browser. – Ankit Agarwal Dec 14 '18 at 06:52
  • i am a complete beginner . Can you please tell me how to do that? – Niraamay Dileep Dec 14 '18 at 06:53
  • JS can not carry over data in that format. If your data isn't overly long, try passing it as a parameter through the url such as `window.open("2.html?data=" + data)`. You will then need to fetch that data from the parameter through JS on `2.html`. – Zitzabis Dec 14 '18 at 06:54
  • @NiraamayDileep check here https://stackoverflow.com/a/32357610/7053190 – Ankit Agarwal Dec 14 '18 at 06:55

1 Answers1

0

Here is code which may help you. In 1.html:

<h1 id="data">Main</h1>
<a href="2.html">
<button onclick="go()">GO</button>
</a>
<script src="action.js"></script>

In 2.html:

<h1 id="display"></h1>
<script src="action.js"></script>
<script>disp();</script>

In action.js:

function go() {
  var data = document.getElementById("data").innerHTML;
  localStorage.setItem("data ", data );
}

function disp() {
  document.getElementById("display").innerHTML = localStorage.getItem("data");
}
Luis Estevez
  • 1,387
  • 12
  • 19