12

How can I find the class name of SVG attribute?

If it was div it would have been {reference to DIV}.className

But, this doesnt work exactly the same way with SVG element

Is there any jquery or javascript method/attribute to get the classes of a SVG element?

See below what I have tried:

enter image description here

Cute_Ninja
  • 4,742
  • 4
  • 39
  • 63
  • Starting from **jQuery 2.2**, you can also use `.hasClass()` from jQuery (your last row from the image). I added a link to the official documentation, a relevant quote and a small example here: http://stackoverflow.com/a/34698009/3885376 – ROMANIA_engineer Jan 09 '16 at 20:03
  • **DO NOT post images of code, data, error messages, etc.** - copy or type the text into the question. https://stackoverflow.com/help/how-to-ask – Rob Nov 21 '19 at 15:55

1 Answers1

27

Here's a jsfiddle: http://jsfiddle.net/Ujp76/

<body>
   <svg class="c1"></svg>

   <script>
      window.addEventListener("load", function () {
          var className = document.querySelector("svg").getAttribute("class");
          console.log(className);
      });
   </script>
</body>
mdlars
  • 909
  • 10
  • 16
  • 27
    Three years ago, this answer prevented me from bankruptcy, saved my mortgage and restored relationships with past friends and family I thought I'd never see again. Now, I've been preparing to deliver a thesis for my PhD in Computer Science and have learned that .getAttribute not only works with getting the class from an svg element but actually ANY attribute from any DOM element. – My Stack Overfloweth Jul 28 '17 at 18:07