I have an input field. When a user focuses on this field, the background of this field will change.
I have a link on my page, too. I want to remove :focus
when the mouse hovers on this link and the user is on that input field.
I use removeClass(':focus')
but this code does not work!
HTML:
<form>
<input type="text" name="email" />
</form>
<a href="#" id="link">Sample Link</a>
Javascript:
$(document).ready(function () {
$('#link').hover(function (){
$('form input[name=email]').removeClass(':focus');
}, function (){
$('form input[name=email]').addClass(':focus');
});
});
CSS:
form input[name=email]:focus {
background-color: yellow;
}
Here is a fiddle for the above
What should I do?
In jQuery by using blur() property we can remove focus from input textbox field.
Use the blur() method to remove the focus from an element, e.g. input. blur() . If you need to remove the focus from the currently active element without selecting it, call the blur method on the activeElement property - document.
To remove or disable focus border of browser with CSS, we select the styles for the :focus pseudo-class. to set the outline style to none to remove the border of the element that's in focus.
You need to use the in-built blur and focus methods:
$(document).ready(function () {
$('#link').hover(function (){
$('form input[name=email]').blur();
}, function (){
$('form input[name=email]').focus();
});
});
Try this:
$(document).ready(function () {
$('#link').hover(function (){
$("input").blur();
}, function (){
//mouse leave
});
});
Working FIddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With