'Can i trigger browser to focus element in JS
When i want to focus on some element I use focus() function which gave me :focus from css. The thing is that when i am focusing with TAB keyboard button i get that styling from css but also some blue styling around element which is probably some browser focus. Can I trigger that blue browser focus somehow too with js?
Solution 1:[1]
You can override this with the focus-visible CSS. Adding sample working example below for reference
button {
background: #F0FF00;
border: 1px solid #ccc;
font-size: 18px;
}
.clear-focus {
background: #00FF00;
}
.clear-focus:focus-visible, .clear-focus:focus {
outline: none;
box-shadow: 0px 1px 6px 0px #000;
}
<input placeholder="click to focus here manually" type="text" />
<p>Now press tab</p>
<button >Button without focus override / default focuss css</button>
<p>Now press tab agian</p>
<button class="clear-focus">Override focuss css / with custom css</button>
isible` CSS class. Sample code below:
Solution 2:[2]
we can use outline: none;,it hides outline
:not(.focusable){
outline: none;
}
<input placeholder="You can't focus me*" type="text" />
<p>press tab</p>
<button class="focusable">You can focus me</button>
<button >You can't focus me*</button>
<p>again repeat</p>
<input placeholder="You can't focus me*" type="text" />
<p>press tab</p>
<button class="focusable">You can focus me</button>
<button >You can't focus me*</button>
<p>*Focus is possible to all element,but border is only hided,as per OP's question</p>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Neptotech -vishnu |
