How to make a button "Close menu"?

0 like 0 dislike
13 views
Given the following code for menu:
$('div.menu').prepend('Open menu'); $("#menu-icon").on("click", function(){ $("ul.menu").fadeToggle(); });


How to make the button "menu-icon" to open settings menu replaced with "Close menu"?
by | 13 views

4 Answers

0 like 0 dislike
$('div.menu').prepend('Open menu'); $("#menu-icon").on("click", function(){ $("ul.menu").fadeToggle(); $("ul.menu").toggleClass("expanded"); if ($("ul.menu").hasClass("expanded")) { $("ul.menu").html("Close menu"); } else { $("ul.menu").html("Open menu"); } });

Well, swap texts if will appear the opposite.
UPD: Changed the code a bit, instead of the $this set $("ul.menu").
by
0 like 0 dislike
Good afternoon.
$("#menu-icon").on("click", function(){ $("ul.menu").fadeToggle(); $(this).text('Close menu.') });
by
0 like 0 dislike
let open = "Open menu"; let close = "Close menu"; $('div.menu').prepend(`${open}`); $("#menu-icon").on("click", function() { $("ul.menu").fadeToggle(); $(this).text((i, text) => text == open ? close : open); });
by
0 like 0 dislike
.toggleClass()
To change the button class as an option on click as an option
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
asked Aug 15, 2019 by partyzanx
0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
110,608 questions
257,186 answers
0 comments
33,710 users