How to remove focus from input field in jQuery?

JqueryHtmlFocus

Jquery Problem Overview


I am setting the focus on a certain input field when loading a page using the following line:

$('#myInputID').focus();

Is there a way that I can undo or remove this focus when hovering over a certain element? (The focus does not have to be reset after leaving this element.)

I couldn't find a function that is the opposite to the above in jQuery or would otherwise work here.

Jquery Solutions


Solution 1 - Jquery

Use .blur().

> The blur event is sent to an element when it loses focus. Originally, this event was only applicable to form elements, such as <input>. In recent browsers, the domain of the event has been extended to include all element types. An element can lose focus via keyboard commands, such as the Tab key, or by mouse clicks elsewhere on the page.

$("#myInputID").blur(); 

Solution 2 - Jquery

check up blur():

$('#textarea').blur()

source: http://api.jquery.com/blur/

Solution 3 - Jquery

For all textbox :

$(':text').blur()

Solution 4 - Jquery

$(':text').attr("disabled", "disabled"); sets all textbox to disabled mode. You can do in another way like giving each textbox id. By doing this code weight will be more and performance issue will be there.

So better have $(':text').attr("disabled", "disabled"); approach.

Solution 5 - Jquery

If you have readonly attribute, blur by itself would not work. Contraption below should do the job.

$('#myInputID').removeAttr('readonly').trigger('blur').attr('readonly','readonly');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2571510View Question on Stackoverflow
Solution 1 - JqueryMilind AnantwarView Answer on Stackoverflow
Solution 2 - JqueryKufView Answer on Stackoverflow
Solution 3 - JqueryMujthaba IbrahimView Answer on Stackoverflow
Solution 4 - JqueryVinayakView Answer on Stackoverflow
Solution 5 - JqueryJeffzView Answer on Stackoverflow