JQuery Programmer Question: Download JQuery Programmer PDF

How can an element be checked if it contains a specific class?

Tweet Share WhatsApp

Answer:

The hasClass method defined can be used to check if an element actually contains the specified class.

For ex : usage of the hasClass:
$("div").click(function()
{
if ( $(this).hasClass("protected"))
$(this)
.animate({ left: -10 })
.animate({ left: 10 })
.animate({ left: -10 })
.animate({ left: 10 })
.animate({ left: 0 });
});

The is() method can also be used with a selector for a more advanced level of matching.
For ex.
if ( $('#myDiv').is('.pretty.awesome') )
$('#myDiv').show();

This method can be used to test various other things, such as it could be used to detect if the specified element is hidden or not.

Download JQuery Programmer PDF Read All 201 JQuery Programmer Questions
Previous QuestionNext Question
What are the guidelines for an application to follow the principles of progressive enhancement?How does jquery store data related to an element?