Is it possible to check if a certain class is present in an element and if yes, create a variable for my function?
<a href="#" id="clickMe"><span class="A open"></span>link</a>
For example if class open is present I will create a varialbe “close”.
$(function ()
{
$('#clickMe').click(function ()
{
// if "open"
var myVar = 'close';
// else
var myVar = 'open';
});
});
yes,
hasClasswill do it:Note that with your specific example, you’ll need something closer to:
since
thisin your callback will refer to theAand youropenclass is on theSPANjQuery also has a
toggleClassmethod that may or may-not be what you’re looking for:For reference: jQuery documentation It’s usually pretty quick to find what you’re looking for.