Is there a way to undo button presses (JS) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Is there a way to undo button presses (JS)

Take YouTube for example. When you click the like button, it likes, click it again, and it unlikes. Is there a way in Javascript where you can replicate that without adding another button?

22nd Jun 2020, 8:54 AM
Clueless Coder
Clueless Coder - avatar
1 Answer
+ 1
In html <p id="p">I dislike</p> <button id="b" onclick="Like.toggle()">Like</button> // in javascript <script> const Like = (function() { let like = false; function toggle() { like = !like; document.getElementById("b").innerText = like? 'Dislike': 'Like'; document.getElementById("p").innerText = like? 'I like': 'I dislike'; } return {toggle}; })(); </script> https://code.sololearn.com/WO7ImdhRX4hL/?ref=app
22nd Jun 2020, 9:27 AM
Calviղ
Calviղ - avatar