I am having problems with jqueries “click” event handler, its working only once, and then it stops, please I need help fixing it.
What I did is, I use an image as a button, for my Web app but, through jquery, but it only runs once.
Here is the code:
JavaScript
x
12
12
1
$( document ).ready(function refresh() {
2
$(".menu_button").click(() => {
3
$("nav").addClass("open-menu");
4
});
5
});
6
7
$( document ).ready(function() {
8
$(".cancel_menu").click(() => {
9
$("nav").removeClass("open-menu").css("transform", "translatex(100%)").css("transition-duration", "2s")
10
});
11
});
12
Please tell me if you need more info, Thanks.
Advertisement
Answer
The problem I don’t think is with the jquery. Your cancel menu moves the menu all the way to the right, but you never bring it back.
I reset the transform to none on the click of the menu button and it works.
Below is the relevant line in the menu_button click event handler
JavaScript
1
2
1
$("nav").addClass("open-menu").css("transform", "none");
2
JavaScript
1
11
11
1
$( document ).ready(function refresh() {
2
$(".menu_button").click(() => {
3
$("nav").addClass("open-menu").css("transform", "none");
4
});
5
});
6
7
$( document ).ready(function() {
8
$(".cancel_menu").click(() => {
9
$("nav").removeClass("open-menu").css("transform", "translatex(100%)").css("transition-duration", "2s")
10
});
11
});
JavaScript
1
3
1
.open-menu{
2
background:red;
3
}
JavaScript
1
4
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<button class="menu_button">OPEN</button>
3
<button class="cancel_menu">CANCEL</button>
4
<nav>NAV</nav>