Design comparison
Solution retrospective
Could anyone tell me how to implement a toggle function without JavaScript?
Community feedback
- @thekooldev1232Posted over 2 years ago
toggle is pretty much a switch between truthy/falsy state of an identifier. To understand in-depth, you can do the following
initialize the default val of a variable You can now toggle using a button click, hover or you can choose the action of your choice Once you choose the action, you can negate the initial state
See below 👇
let toggleVal = false;
function changeToggle() { toggleVal = !toggleVal; }
Now in your HTML, you can call code like this
<button OnClick=changeToggle()>ToggleButton</button>
1@TSune-webPosted over 2 years agoHi @thekooldev1232,
Thank you for the example! I'll try it.
0 - Account deleted
hi there,
- to add interactivity to your website you would always need to use javascript but you could do something similar to onclick/toggle with css using hover selector you can read more about it here
i hope this is helpful and goodluck
1@TSune-webPosted over 2 years agoThank you @Old1337!
I did a bit of research on my own and saw some people using pseudo-classes such as ": active" or ": checked". I also thought about the ": hover" class, but the properties with this class are only applied while the cursor is hovering over the component. So, I suppose ": hover" should be used with other pseudo-classes for such functionality, maybe?
Anyway, as you mentioned, I'll learn JavaScript for interactivity. Thank you for the helpful advice and suggestion!
Happy coding💻✨
1
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord