0

Trying to get some help on some JavaScript we are trying to post accross multiple landing pages. We are sending values to hidden fields on a button click 'lp-pom-button-3'.

However, the ID changes depending on the page, but will always have the format 'lp-pom-button-' with the number potentially changing depending on the page. Is there a function similar to 'like' in SQL, or saying any button that starts with or contains 'lp-pom-button-" for this to be triggered on? Thanks for you help!

<script type="text/javascript">
document.getElementById('lp-pom-button-3').addEventListener(
'click', function(event) {
  ga(function() { 
    var tracker = ga.getAll()[0];
    var clientId = tracker.get('clientId');
    document.getElementById('GACLIENTID').value = clientId;
    var userId = tracker.get('userId'); 
    document.getElementById('GAUSERID').value = userId; 
  });
});

jgeeda
  • 33
  • 2

1 Answers1

2

You can use the css selector for attribute value contains.

[id*="lp-pom-button-"]

or attribute value starts with:

[id^="lp-pom-button-"]

In vanilla javascript you can access elements using css selector with querySelector or for getting a list of elements querySelectorAll.

Here is an example:

document.querySelector('[id^="lp-pom-button-"]');

And HERE is a list of all the browsers and browsers version that support this function.

Titus
  • 22,031
  • 1
  • 23
  • 33