0

I'm trying to get some dynamic text working on a landing page I've built with leadpages. Im no javascript wizard so please bear over with me.

I would like to get the URL parameter ?salon=NameOfSalon so I can automatically change the text accordingly and when empty some default text.

Here is what I got so far

function getParameterByName(name, url = window.location.href) {
    name = name.replace(/[\[\]]/g, '\\$&');
    var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, ' '));
}

var salon = getParameterByName('salon');

document.getElementById('salon').innerText = getParameterByName('salon');

But it only changes it in one place, not all of them and there is no default text when empty (I've used <span id="salon"></span>)

Hope it makes sense!

3
  • IDs are supposed to be unique. If you have multiple elements to process, use a class instead, and loop over them (with a for loop for example) Commented Jun 1, 2021 at 19:58
  • Does this answer your question? How can I get query string values in JavaScript? Commented Jun 1, 2021 at 20:08
  • @cbender I've looked at it and my knowledge on this is sadly so limited that I can't figure out how to use their examples. But thanks for the link and appreciate your answer. Commented Jun 1, 2021 at 21:16

1 Answer 1

2

The id property should be unique for each HTML element. If you'd like to apply the new innerText to multiple elements, try assigning each element a known class, and then using getElementsByClassName to get references to them all.

e.g.,

var elements = document.getElementsByClassName('salon-class');
for (var element of elements) {
  element.innerText = getParameterByName('salon') || ''
}

This assumes your HTML defines elements like: <span class="salon-class"></span>.

Sign up to request clarification or add additional context in comments.

4 Comments

I've tried to insert your code here function getParameterByName(name, url = window.location.href) { name = name.replace(/[\[\]]/g, '\\$&'); var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'), results = regex.exec(url); if (!results) return null; if (!results[2]) return ''; return decodeURIComponent(results[2].replace(/\+/g, ' ')); } var elements = document.getElementsByClassName('salon-class') for (var element of elements) { element.innerText = getParamterByName('salon') || '' } But can't get it to work. I'm not strong in js - sorry
I see that there had been a typo in my answer ("getParamterByName" vs "getParameterByName") that was copied into your code. If continue to have problems after fixing that, we'd have to see what error message(s) you are receiving.
That fixed it - Thanks a lot! Is there somewhere I can insert a default text? If someone follows the link without having a URL parameter?
You can supply default text with the || '' trick I added in there; just replace the '' with your default value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.