JQuery Programmer Question:
How can an element be checked if it contains a specific class?
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.
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.
Previous Question | Next Question |
What are the guidelines for an application to follow the principles of progressive enhancement? | How does jquery store data related to an element? |