In vanilla JS, I want to take a value and append it to the href value of a link, but for reasons I can’t understand the value is being added twice.
My HTML is:
<a href="some-path/some-page" class="js-btn">Click me</a>
My Javascript is:
var refCode = "1234";
var href1 = document.querySelector(".js-btn").href;
document.querySelector(".js-btn").href = href1 + "?referral_code=" + refCode;
console.log(href1);
The expected result is:
<a href="some-path/some-page?referral_code=1234" class="js-btn">Click me</a>
but the actual result is:
<a href="some-path/some-page?referral_code=1234?referral_code=1234" class="js-btn">Click me</a>
Of course, I’ve checked to see if the script is appearing twice, but it’s not.
Would anyone know why this is?