jQuery addClass example

Change class name on click in jQuery

by Sam Liew

HTML

<a href="https://gog.com" target="_blank">GOG.com home page</a>
<p>some text</p>
<a href="https://www.gog.com/about_gog" target="_blank">Another GOG.com link</a>
<p>more text</p>
<a href="https://www.gog.com?pp=another-affiliate-id" target="_blank">This link already has an affiliate id, it doesn't add a second time</a>

JavaScript

// Change "my-affiliate-id" below to your actual affiliate id
const aid = 'my-affiliate-id';

// Append slash with affiliate id, only if an affiliate ID is not found in the link yet
const goglinks = document.querySelectorAll('a[href*="gog.com"]');
goglinks.forEach(function(el) {
  if(!el.href.includes('pp=')) {
    el.href = el.href.replace(/\?.*$/, '') + '?pp=' + aid
  }
})