0

I'm trying to get the className of an element using JSDOM. I am able to find the element and I'm sure it exists. I've tried looking online but can't find an example of how to find the className of said element using JSDOM. Any help would be appreciated.

delta-alph
  • 21
  • 2
  • 6
  • but based on what? you want to to find the classname of elemet, but on what element?? if ever you got the element and you need its classname check this out https://stackoverflow.com/questions/7796127/how-to-get-class-name-through-javascript – Jovylle Jun 16 '20 at 02:44
  • I am finding the element with descendants selectors, something like `div > a`, but I need to find the class name of the `a` tag. It's class changes so it has to be dynamic. I've tried using `.className` in JSDOM but it outputs an object instead of a string. – delta-alph Jun 16 '20 at 11:25

2 Answers2

0

When you got your element and you wanted its classname you can use className from element object.

var element = document.getElementById("demo");
console.log(element.className)

<!DOCTYPE html>
<html>
<body>

<p id="demo" class="sample_class"></p>

<script>
console.log(document.getElementById("demo").className);
</script>

</body>
</html>
Jovylle
  • 859
  • 7
  • 17
0

Ok I found the problem. In JSDOM you need to use element.className.baseVal to get classes as a string.

delta-alph
  • 21
  • 2
  • 6