最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - which js event is used to automatically remove focus from the input field? - Stack Overflow

programmeradmin4浏览0评论

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

Share Improve this question edited Aug 10, 2016 at 7:40 M.Tanzil 2,0051 gold badge14 silver badges28 bronze badges asked Aug 10, 2016 at 6:29 Deepanshu GeraDeepanshu Gera 431 silver badge4 bronze badges 1
  • 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
Add a ment  | 

3 Answers 3

Reset to default 3

You 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"/>

发布评论

评论列表(0)

  1. 暂无评论