Amit Turgeman
Amit Turgeman

Reputation: 7

How to pass a variable to a new page and insert it to a textbox?

I am trying to transfer a variable from one page using this code:

<a href="login.html" onclick="NameFunction('asdf');">Edit</a>

to the page login.html, and insert the variable(asdf in this example) to a textbox in the new page.

I have tried this code:

function NameFunction(name) {
    document.getElementById("username").value = name;
}

It is not working. I think it doesn't work because thats another page.

Upvotes: 1

Views: 41

Answers (2)

Himanshu Tanwar
Himanshu Tanwar

Reputation: 906

you can try using localStorage.

  1. In you're onClick method, save the data you want to store in localStorage

    localStorage.setItem(key,value);

  2. When the new page loads, in the onLoad (ready function if using jquery) method get the data from localStorage and set it in the textbox

you can find more about localStorage in the following links

Storing Objects in HTML5 localStorage

http://www.w3schools.com/html/html5_webstorage.asp

and many more are available online

Upvotes: 0

Mikhail Romanov
Mikhail Romanov

Reputation: 1612

If you want to pass parameters to another page you can put them using query string:

<a href="login.html?param=asdf">Edit</a>

To get parameter on the login.html page you can use jQuery as it described here: Get escaped URL parameter

Upvotes: 1

Related Questions