0

I want to be able to extract a certain part of the classes only if it contains a certain value.

Let's say I have <div class="aa bb blog-xxx"></div>

I want to be able to only extract blog-xxx

{{Click Classes}} is a variable from Google Tag Manager

I have tried the following code:

function blogChecker(string) {
  var el = {{Click Classes}};
  if (el.includes("blog")) {
    return el;
  }
}

which extracts "aa bb blog-xxx" but I only want "blog-xxx"

Update I got it to work using the following code:

function() {
  var el = {{Click Element}};
  var regex = /blog.*/;
  var classes = el.className.match(regex);
 return classes;
}
2
  • 3
    You can use the class list API to find the class names individually. Commented Jan 6 at 19:39
  • 1
    I think you're looking for indexOf, eg: el.indexOf('blog') Commented Jan 6 at 19:47

1 Answer 1

0

For {{Click Element}}

You can use the classlist

For example

<div class="aa bb aabbcc-aabbcc"></div>

This element's classlist is

element.classList; // ['aa', 'bb', 'aabbcc-aabbcc']

And if you want to check the element class contains "bb"

element.classList.contains('bb'); // true

Here you go!

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

Comments

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.