Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
Appending current URL parameters onto anchor link

At a page like

https://www.example.com/?firstname=Steven&lastname=Smith&email=steve%40gmail.com&phone=0404555555

I have a button (anchor link) #ptsBlock_553944 .ptsCell:nth-of-type(1) .ptsEditArea.ptsInputShell that links to https://www.example.com/form

I'd like to append the URL parameters from the current URL to the button's URL, so that the button's href is now https://www.example.com/form/?firstname=Steven&lastname=Doig&email=steve%40gmail.com&phone=0404555555

How can I do this with JavaScript please?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Use window.location.search:

var url = "https://exmaple.com";
var newurl = url + window.location.search;

newurl will contain all the get (ex. ?something=something&something2=something5) data.

To change the href of a:

var button = document.getElementById('#ptsBlock_553944');
button.href = button.href + window.location.search;
about 4 years ago · Juan Pablo Isaza Report

0

If you don't care about supporting older browsers you can use the URL API and URLSearchParams.

function appendCurrentUrlSearchParams(anchorElement) {
  const currUrlSearchParams = new URL(window.location.href).searchParams;
  const link = new URL(anchorElement.href);

  // uncomment this line if you want to clear query parameters already present in the anchor url
  // link.search = '';

  for (const entry of currUrlSearchParams.entries()) {
    link.searchParams.append(entry[ 0 ], entry[ 1 ]);
  }
  
  anchorElement.href = link.href;
}

Usage in your case:

appendCurrentUrlSearchParams(document.querySelector('#ptsBlock_553944 .ptsCell:nth-of-type(1) .ptsEditArea.ptsInputShell'));
about 4 years ago · Juan Pablo Isaza Report

0

Read Html select using select to change the link of a button with Javascript

specifically the section on

  1. Get the element with something like document.getElement MDN getElement Link

  2. Change the .href of that element to what you want.

function selectFunction() {
    var x = document.getElementById("selectopt").value;

    document.getElementById("mylink").innerHTML = x;
  
    document.getElementById("mylink").href = "http://www." + x + ".com";
  
}
document.location.pathname = '/questions/69240453/appending- 
current-url-parameters-onto-anchor-link/69240510';

//get the document pathname I chose from document.location
let data = document.location.pathname;

let preUrlString = 'www.example.com/form';

let newString = preUrlString + data;

console.log(newString);
'www.example.com/form/questions/69240453/appending- 
current-url-parameters-onto-anchor-link/69240510'

document.getElementById("mylink").href = newString;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!