i have a hamburger menu. This is how it looks like:
<input type="checkbox" id="toggle">
<label for="toggle" class="bigmac">☰</label>
I used a symbol for the icon. I want to change the icon when clicking on it to a other icon, a cross. And when i click again, to close the menu, change it back to the hamburger.
I tried a lot but have no idea how to do this using the "toggle".
This what i have in jquery to change it to the cross:
$( ".bigmac" ).click(function(){
$( ".bigmac").html("✕")
})
But how do i let it change back when clicking again?
You can add a class cross
into the .bigmac
element so that you can have a if-else
condition setup accordingly to get toggle effect:
$( ".bigmac" ).click(function(){
if($(".bigmac").hasClass('cross')){
$(".bigmac").html("☰").removeClass('cross');
} else {
$(".bigmac").html("✕").addClass('cross');
}
})
.cross{
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="toggle">
<label for="toggle" class="bigmac">☰</label>