'get div class name of context in chrome extension

I am trying to make my first chrome extension with manifest version 3. I am trying to get info about the open tab page. I figured out how to get all the Header-tags (H1,H2,H3,...) of the context.

$('h1, h2, h3, h4, h5, h6', context).each(function() {
            headings.push({
                text: ($(this).text() || '').toString().trim(),
            });
        });

But I want to get the className of all div-tags.

When I do

$('div', context).each(function() {
            divs.push({
                text: ($(this).text() || '').toString().trim(),
            });
        });

I only get the info between the tags

<div>I get this </div>

but I want to get "classname"

<div class="classname"></div>


Solution 1:[1]

It happens because you try get text value of the elements in side the loop . Please use

    $('div', context).each(function() {
            if($(this).hasAttribute("class") {

               var cls = $(this).getAttribute("class")
               divs.push({
                     text: cls ,
                      });
          }
            
        });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Osman Corluk