October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

How to Pass an Input Value from One Page to Another with JavaScript

A page transition does not carry JavaScript variables or DOM values with it. Use a query parameter for a simple, non-sensitive handoff, a server-handled form, or browser storage.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A new page does not automatically receive the previous page’s HTML elements or JavaScript variables. To carry an input value forward, submit it to a server, put a non-sensitive value in the destination URL, or store it in the browser. For a simple store-locator flow, a query parameter is often the shortest path; use a server-handled form or browser storage when the application’s needs call for it.

Why the next page cannot read the previous page’s input

When navigation loads another document, the first page’s DOM and ordinary JavaScript variables are not carried over. An input’s id identifies an element within its own document; repeating that ID on the destination page does not transfer its value. The pages need an explicit handoff.

This distinction matters in the store-locator example: the form submits a field named address to ehound.php, while the locator code looks for an element with ID address. Those are different identifiers serving different purposes. The server or destination-page code must connect the submitted value to the locator’s search function. The original example is discussed in a 2011 Stack Overflow question.

Choose a handoff method

Method Best fit Trade-off
URL query parameter A small, non-sensitive value that should be visible or shareable in a link Appears in the address bar and can remain in browser history or copied links
Form submission to a server The application already has an endpoint that can process the form and render or initialize the next page Requires server-side handling; a form submission alone does not populate an unrelated static page
sessionStorage Client-side data needed across page loads in the same tab session Not part of the URL, but scoped to browser storage context and session behavior

For several steps of a form, preserve earlier values deliberately rather than replacing the existing query state or building a chain of hidden inputs. A related multi-page form discussion considers browser storage and passing values between pages.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Pass a non-sensitive value in the URL

Use URLSearchParams to encode the value safely for the query string, and read the same parameter on the destination page. MDN documents the browser API in its URLSearchParams reference.

On the first page

Give the input a useful name or ID, read its value, and construct the destination URL. This example assumes the destination is map.html in the same site:

<label for="address">Address or ZIP code</label>
<input id="address" name="address">
<button type="button" id="find-location">Find locations</button>

<script>
document.getElementById("find-location").addEventListener("click", () => {
  const address = document.getElementById("address").value;
  const params = new URLSearchParams({ address });
  window.location.href = `/map.html?${params.toString()}`;
});
</script>

URLSearchParams handles characters such as spaces and punctuation when creating the query string. Avoid concatenating raw input directly into a URL, since characters in the value can alter how the URL is parsed.

On the destination page

Read the parameter from window.location.search, then pass it to the locator’s own search function. The function name below is illustrative; use the actual function provided by the locator:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const params = new URLSearchParams(window.location.search);
const address = params.get("address");

if (address) {
  searchLocations(address);
}

Reading a value is only the handoff. The destination still needs code to put it into the locator or otherwise use it; the query parameter does not automatically populate an input or launch a search.

Submit the form to a server

If the application already uses an endpoint such as ehound.php, keep the handoff in that request flow. The form field must have a name, because the server receives submitted field names and values. For example:

<form action="ehound.php" method="post">
  <label for="address">Address or ZIP code</label>
  <input id="address" name="address">
  <button type="submit">Find locations</button>
</form>

The server endpoint must read the submitted address and render the locator page with that value available to its markup or JavaScript. Simply setting the form’s action to an endpoint—or using the same element ID on the next page—does not by itself pass data into a separate static document. A POST-based flow is also preferable to placing sensitive values in a URL, but the application still needs appropriate server-side handling.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Keep a value in browser storage

Use sessionStorage when the value should remain client-side and be available after navigation in the same tab session. The browser API is documented in MDN’s sessionStorage reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
// Before navigating
sessionStorage.setItem("address", document.getElementById("address").value);
window.location.href = "/map.html";

// On map.html
const address = sessionStorage.getItem("address");
if (address) {
  searchLocations(address);
}

Choose storage based on how long the value should persist and the context in which it must be available. localStorage is another client-side option for longer-lived persistence; do not use it by default when a value only needs to survive one page transition.

Keep personal or sensitive values out of URLs

Query parameters can be exposed in the address bar, browser history, and links that a user copies or shares. Do not put sensitive personal data in a URL. Use an appropriately designed POST flow or server-side session for values that should not travel as a visible link. Browser storage is also client-side; choose it only when its scope and handling are suitable for the data.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.