Skip to main content
JavascriptSnippets

Append Parameter to Anchor HREF by Class

By May 30, 2020No Comments

I wanted the parameter of a URL to append itself to anchor href by class like https://somedomain.com/?ref=1 the param being ?ref=1 from URL being appended to <a href=”https://anotherdomain.com/”>my link</a> so when the link is clicked the new URL is https://anotherdomain.com/?ref=1

	var x, i;
	x = document.querySelectorAll(".param");
	for (i = 0; i < x.length; i++) {
		x[i].href += window.location.search;
	}

All I need to do on somedomain.com is assign a special class to each link I want the parameter included with.

<a href="https://anotherdomain.com/" class="param">my link</a>

Contact us to learn more about Adopt the Web for your business

Author Jarod Thornton

More posts by Jarod Thornton