Hi guys i got an <a> tag which look like this:
<a class="js-swatch-item-link swatch__item-inner-image" data-layer-click="{"event":"interaction","interaction":{"category":"colorPattern","action":"Select","label":"DARK GREY MELANGE","value":"DARK GREY MELANGE"}}}" href="#">DARK GREY MELANGE</a>
I am trying to fetch the value of the interaction section of the JSON string, and then afterwards assigning this value to the innerHTML of the <a> tag.
My JavaScript code looks like this:
var SWATCH_COLOR_LINK = '.swatch.colorpattern a';
var swatchColorLink = document.querySelectorAll(SWATCH_COLOR_LINK);
var swatchColorTitle = JSON.parse(swatchColorLink.getAttribute('data-layer-click')).interaction.value;
for (var i = 0; i < swatchColorLink.length; i++) {
swatchColorLink[i].innerHTML = swatchColorTitle;
}
The function is giving me the following error: swatchColorLink.getAttribute is not a function
However if I try to just select the first element like this: document.querySelectorAll(SWATCH_COLOR_LINK)[0]
The function works fine and sets the right value from the JSON string to the innerHTML of the <a> tag.
Im guessing im doing something wrong in my loop to go over all <a> tags and assigning them the value of their JSON string.
forloop, you know thatswatchColorLinkcontains a collection of elements, in the.getAttributeline, however, you don’t…?