+ 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?
2 Answers
+ 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