Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery remove focus from textbox [duplicate]

Tags:

jquery

How do I remove the focus of a TextBox in JQuery?

Edit: This is the solution I was looking for:

$(this).trigger('blur');

For an unknown reason, $(this).blur(); did not work. I created a new, clear solution and then it worked.

like image 511
Kohnarik Avatar asked Jul 21 '26 05:07

Kohnarik


2 Answers

JS Fiddle

$(function () {
    $('input[type=text]').focus(function () {
        $(this).val('');
        // add the below line to trigger the blur event
        $(this).trigger('blur'); 
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<input type="text" value="abcdef"> 
like image 110
Mi-Creativity Avatar answered Jul 22 '26 19:07

Mi-Creativity


you can force it to blur using trigger function

$("...").trigger("blur");
like image 23
Emad Armoun Avatar answered Jul 22 '26 19:07

Emad Armoun