george george - 2 months ago 9
jQuery Question

jquery check if element has empty class

Hello guys i have a problem how to check if element has empty class ... I tried the code below , but its not working. Some advices would be welcomed.

<nav id="main-nav" class="">
<ul>
<li><a href=""><i class="fa fa-pencil-square-o" aria-hidden="true"></i> references</a></li>
<li><a href=""><i class="fa fa-male" aria-hidden="true"></i> about me</a></li>
<li><a href=""><i class="fa fa-wrench" aria-hidden="true"></i> abilities</a></li>
</ul>
</nav>


if ($('#main-nav:not([class])')){

$(this).addClass("nav-type1");

}

Answer

You can merely do this:

var $elem = $('#main-nav');
if (!$elem.attr('class')) {
    $elem.addClass('nav-type1');
}

where I think that comments are not necessary...