Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an HTML element has no class

I want to append a class to an anchor tag, which as no class. Also this anchor tag is inside many DIV elements and h4 tag So h4 has a class say abc inside this h4 tag there is this anchor tag which doesn't have any class.This is what I tried but not working. I want to append a class to the anchor that doesn't have a class at present.

$(document).ready(function(){
 $('.abc a').hasClass(''){
   $(this).addClass('active');
 }
});

FIDDLE

like image 444
Leroy Mikenzi Avatar asked Mar 06 '14 13:03

Leroy Mikenzi


People also ask

How do you check if an element is empty in HTML?

Method 1: Using the “:empty” selector: The element to be checked using is() method. The is() method is used to check if one of the selected elements matches to the selector element. This method can be used on this element to test if it is empty by using “:empty” selector.

Can the HTML element have a class?

HTML elements can have an id and/or class attribute. The id attribute assigns a name to the element it is applied to, and for valid markup, there can be only one element with that name. The class attribute assigns a class name to the element, and that name can be used on many elements within the page.

How do you check whether a class is available or not?

Method 1: Using hasClass() method: The hasClass() is an inbuilt method in jQuery which check whether the elements with the specified class name exists or not. It returns a boolean value specifying whether the class exists in the element or not.


1 Answers

You could use:

$('.abc a').filter(':not([class])').addClass('active');
like image 141
A. Wolff Avatar answered Sep 30 '22 17:09

A. Wolff