Change Html button color when user unselect button | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Change Html button color when user unselect button

How to change Html button color when user unselect button using Js and CSs

11th Apr 2020, 4:24 PM
Souptik Nath
Souptik Nath - avatar
9 Answers
+ 2
so you want to style it differently based on its focused/blurred state ;) with css it's easy, as we have the pseudo-classes :hover and :not... button { outline: blue solid 2px; /* default style for all states */ } button:not(:focus) { outline: red solid 2px; /* style for blurred state */ } button:focus { outline:lile solid 2px; /* style for ficused state */ } Obviously, if you use all three rules, the first one will never apply ;)
11th Apr 2020, 5:29 PM
visph
visph - avatar
+ 2
You can create additional class for this button and for example toggle this class with an event of it.
11th Apr 2020, 4:37 PM
JaScript
JaScript - avatar
0
Please share a short code for this
11th Apr 2020, 4:42 PM
Souptik Nath
Souptik Nath - avatar
0
What do you mean by "unselect button"? Buttons are not able to be select/unselect... Radio buttons or checkboxes can be selected/unselected, but cannot be styled (as workaround, you could hide them and build a real html element based on their states) ^^ However, button can be focused/blurred (currently active or not: active element receive keyboard default action such as click on return key pressed)...
11th Apr 2020, 5:21 PM
visph
visph - avatar
0
visph I mean that when the user clicks the buttom then clicks on body
11th Apr 2020, 5:22 PM
Souptik Nath
Souptik Nath - avatar
0
visph How can I use the same property in Js For example: When button in focus{ Some statement } When button not in focus{ Some statement }
11th Apr 2020, 6:22 PM
Souptik Nath
Souptik Nath - avatar
0
With JS you need to handle the 'focus' event (and/or the converse 'blur' event): https://developer.mozilla.org/en-US/docs/Web/API/Element/focus_event Another way, less efficient but maybe easiest if event handling is unknown for you, would be to use setInterval or setTimeout, for watching the document.activeElement regularly: https://developer.mozilla.org/en-US/docs/Web/API/DocumentOrShadowRoot/activeElement
11th Apr 2020, 7:06 PM
visph
visph - avatar
12th Apr 2020, 5:49 PM
Souptik Nath
Souptik Nath - avatar
0
visph Please help
12th Apr 2020, 6:14 PM
Souptik Nath
Souptik Nath - avatar