Hi everyone,
Basically, when you click on the lens icon, it shows a hidden search bar, and if you click again on the lens, it hides it back. How can I achieve the same result, (hiding it) by clicking on the body, hence anywhere outside the icon itself?
Hope it was clear, thanks in advance. LINK
P.S. To add the transition, I added the class “active” to the div, and through JS I simply toggle it with a click event.
Well, that is the same behavior as the original code (using class toggle). But it’s definitely something to consider.
Not really sure what the intention of the button is. Is it just to toggle open the search or will it be used as an actual submit button inside a form? The tab order is also a little odd with the current code as you focus the input before the button but you can’t see the input.
Not really sure how to best handle both cases. I was playing around with the relatedTarget property but I’m not really sure I have thought this through. https://codepen.io/lasjorg/pen/VwKqWeY
You’re right; I was just following along with a Udemy course on JS small projects, and I wanted to play around with it and add some extra functionality. So I’m not sure what the purpose of the button would be, either. So you definitely have a point.
When it comes to relatedTarget, could you briefly explain it or give me a good resource? I’m a beginner and I haven’t touched on that one. Thanks a lot!
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
You can look at the MDN page for it, the table they have explains it.
As an example. If you listen for blur on an input and tab to a button, the relatedTarget will be the button. If you listen for focus on an input and tab from a button, the relatedTarget will be the button.