Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
182
rated 0 times [  189] [ 7]  / answers: 1 / hits: 32045  / 10 Years ago, wed, august 13, 2014, 12:00:00

I'm looking for a robust way of dynamically injecting a query string parameter when a visitor clicks on a static anchor-link.



For example a link:



<a href=http://www.johndoeslink.com>Test</a>


I want to pass a query string to the next page but have to assign the value dynamically. How can I achieve this?



I know this is simple I'm just missing something obvious! =



Thanks in advance,



John d


More From » html

 Answers
22

There are many ways you could do this. Below I've listed two very simple methods. Here I'm assuming you already have a reference to your a element (here I've called this element):



Modifying the href attribute



element.href += '?query=value';


Using a click event listener



element.addEventListener('click', function(event) {
// Stop the link from redirecting
event.preventDefault();

// Redirect instead with JavaScript
window.location.href = element.href + '?query=value';
}, false);

[#69797] Monday, August 11, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
samsons

Total Points: 331
Total Questions: 97
Total Answers: 106

Location: Macau
Member since Mon, Nov 16, 2020
4 Years ago
;