As, I have input field in my site, in which I have to scan the bar-code
and taking that array to process further, how do I process that after scanning by scanner automatically(not by scrolling mouse or click outside the text box, similarly, as done in any departmental or value store)?basically I am looking for the JavaScript event which automatically loses focus or help me to do the same like in departmental store after scanning?
I have used the event onblur()
but after scanning I have to shift the focus by clicking outside the input field . I want it to be done automatically
As, I have input field in my site, in which I have to scan the bar-code
and taking that array to process further, how do I process that after scanning by scanner automatically(not by scrolling mouse or click outside the text box, similarly, as done in any departmental or value store)?basically I am looking for the JavaScript event which automatically loses focus or help me to do the same like in departmental store after scanning?
I have used the event onblur()
but after scanning I have to shift the focus by clicking outside the input field . I want it to be done automatically
-
An event is something that happens to you, not something that you do. To remove focus, you can set the focus to something else like the document, with
document.focus()
. – user663031 Commented Aug 10, 2016 at 6:34
3 Answers
Reset to default 3You can also do
setTimeout(function() {
document.getElementById('link').blur();
},2000);
You can use jQuery library, blur() and trigger() functions in it
See this
$('#but').on('click', function(){
$('input').trigger('blur');
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" autofocus>
<button id="but">Lose Focus</button>
Or in pure Javascript you can do
document.getElementById('get').addEventListener('click', function(){
document.getElementById('link').focus();
})
document.getElementById('lose').addEventListener('click', function(){
document.getElementById('link').blur();
})
<a id='link' href="www.google.">Simple Link<a/>
<button id="get">Get Focus</button>
<button id="lose">Lose Focus</button>
In JavaScript you can use the blur event to remove focus from the text box or an element
(function () {
'use strict';
var textbox = document.getElementById('myTextBox');
var btn = document.getElementById('loseFocusBtn');
textbox.focus();
btn.addEventListener('click', btnClick);
function btnClick() {
textbox.blur();
}
})();
<input type="text" value="Hello, World!" id="myTextBox"/>
<input type="button" value="Lose Focus" id="loseFocusBtn"/>