How to check for a class in jquery
Hello, how can I check if a block has a class using jquery?
I tried it like this, but it doesn't work
if ($('.menu-on')) {
$(".burgers__item").click(function () {
$(this).removeClass("menu-on");
});
} else {
$(".burgers__item").click(function () {
$(this).addClass("menu-on");
});
}
$('.burgers').click(function () {
$('.menu .menu__wrap').slideToggle();
});
0
1 answers
Function .hasClass (), a simple example, outputs a message about the presence of the test1 class in the element when clicked:
$('div').click(function(){
if($(this).hasClass('test1')){
alert('У этого блока есть класс test1');
}else{
alert('У этого блока нет класса test1');
}
});
.test1{
width:100px;
height:100px;
padding:10px;
margin:10px;
background: red;
}
.test2{
width:100px;
height:100px;
padding:10px;
margin:10px;
background: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="test2"></div>
<div class="test1"></div>
<div class="test2"></div>
<div class="test2"></div>
3
Author: ExposedCat, 2017-12-25 09:01:30