para club101 para club101 - 4 months ago 11x
HTML Question

how to transfer data from one html page to another using javascript

I'm trying to transfer data from one html page to another page, so I wrote this program:


<a href="/events" onclick="send()">
<span class="message">
new alert

function send() {
$mesg = $('.message');
var msg = $mesg.text();
var msg1 = msg.replace(/\s/g, '');
localStorage.setItem("message", msg1);


<script src=""></script>
document.getElementById("event").innerHTML = localStorage.getItem("message");
<td id="event"></td>

But I don't get the data transferred from the pages? what is my mistake?


The issue is in page2.ejs:

Problem: The HTMLElement <td id="event"></td> is not yet available when your <script> block is parsed and executed, so document.getElementById("event") returns undefined.

Solution: Move the <script> block to the bottom, beneath <td id="event"></td> and it should work.