Categories
GTM Javascript

Use Custom Javascript Variable in GTM to get multiple elements

The problem: I need to put all the elements that contain the class “has-error” inside a variable

In order to do it I need to return a string with all the elements ids concatinated.

function() {
  var els = document.querySelectorAll('li.has-error');
  var ids = []
  for (var i = 0; i < els.length; i++) {
    ids.push(els[i].getAttribute('id'));
  }
  return ids.join();
}