Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove :focus with jquery

Tags:

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?

like image 942
MajAfy Avatar asked Jan 22 '14 09:01

MajAfy


People also ask

How Stop focus in jQuery?

In jQuery by using blur() property we can remove focus from input textbox field.

How do you remove focus from 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.

How do you turn off focus in CSS?

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.


2 Answers

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();
    });
});
like image 118
CodingIntrigue Avatar answered Oct 12 '22 13:10

CodingIntrigue


Try this:

$(document).ready(function () {
$('#link').hover(function (){
     $("input").blur(); 
 }, function (){
      //mouse leave  
});
});

Working FIddle

like image 32
Milind Anantwar Avatar answered Oct 12 '22 12:10

Milind Anantwar