Total Page Preview:   000000000126

What is addClass() and hasClass() in jQuery?

 jQuery addClass() and hasClass()

In jQuery, addClass() and hasClass() are methods used for manipulating and checking CSS classes on HTML elements.

1-addClass() Method:
The addClass() method is used to add one or more classes to the selected elements.
Syntax: $(selector).addClass(className1, className2, ...);
In this example, the addClass() method is used to add the class "highlight" to the paragraph with the ID "myParagraph". This class has a corresponding style in the <style> block that sets the background color to yellow.
 
2-hasClass() Method:
The hasClass() method is used to check if the selected elements have a specified class.
Syntax: $(selector).hasClass(className);
In this example, the hasClass() method is used to check if the paragraph with the ID "myParagraph" has the class "highlight." The result is then logged to the console.
 
 
 
 

Thank You

About Author

Brijesh Kumar

Database Developer

I have more then 6 years Experience in Microsoft Technologies - SQL Server Database, ETL Azure Cloud - Azure SQL Database, CosmosDB, Azure Data Factory, PowerBI, Web Job, Azure Function, Azure Storage, Web Apps, Powershall and Database Migration On-Premise to Azure Cloud.
LinkedIn : https://www.linkedin.com



Comments