White Arrow icon
Back to all Elements

Pass Parameters

Pass input fields parameters and add them to the destination URL.

2 JavaScripts to pass parameters in the URL.

First script, takes the inputs in a form, and on form submit, adds them to the destination URL.

document.getElementById("send-form").addEventListener("submit", function(e){
    e.preventDefault();


    var fnameValue = document.getElementById("fname").value;
    var lnameValue = document.getElementById("lname").value;
    var emailValue = document.getElementById("email").value;
    var compValue = document.getElementById("company").value;

    window.location = window.location + "catch" + "?fname=" + fnameValue + "&lname=" + lnameValue + "&email="+ emailValue + "&comp=" + compValue;

});

Copy

The 2nd part catches the parameters from the URL and populate the form fields:

var url_string = window.location.href;
var url = new URL(url_string);

var fname = url.searchParams.get("fname");
var lname = url.searchParams.get("lname");
var email = url.searchParams.get("email");
var comp = url.searchParams.get("comp");

document.getElementById('fname').value = fname;
document.getElementById('lname').value = lname;
document.getElementById('email').value = email;
document.getElementById('company').value = comp;

Copy

Preview:

browser mockup
Share:
Heart icon

Whenever possible, use Class Names to target the elements in your interaction. You never know when you'll need to duplicate and use it somewhere else...

Might also interest you:

CMS Autocomplete

Cloneable
CMS
JavaScript

A short JavaScript code for transferring a CMS list into an autocomplete on an input field.

Read more
Blue arrow iconWhite Arrow icon

Auto get Current Year

Cloneable
Code
JavaScript

Automatically insert the current year to any element with the class of 'year'

Read more
Blue arrow iconWhite Arrow icon

Scroll Menu to Current Link

Cloneable
CMS
Code

Using JS to scroll a div (menu) to the current link inside.

Read more
Blue arrow iconWhite Arrow icon