admin管理员组

文章数量:1333442

I wonder how to set the focus to an input field on the mouseover event.

I wonder how to onload input field or onfocus field for typing when onmouseover to it so it easily ready to type when cursor touching or onmouseover cursor to input field of search box.

I wonder how to set the focus to an input field on the mouseover event.

I wonder how to onload input field or onfocus field for typing when onmouseover to it so it easily ready to type when cursor touching or onmouseover cursor to input field of search box.

Share edited Oct 6, 2016 at 17:32 Enamul Hassan 5,44523 gold badges43 silver badges58 bronze badges asked Oct 6, 2016 at 16:25 Keta SambungbyrKeta Sambungbyr 311 gold badge1 silver badge2 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 4

You can use the focus() method to focus an HTML element. Here's an example of its usage:

var inputField = document.getElementById('idOfYourInputField');

inputField.addEventListener('mouseover', function() {
    inputField.focus();
});

Using jQuery:

$('.input-element').mouseover(function() {
    $(this).focus();
})

Or if you want to control the focus and blur events

$('.input-element')
    .mouseenter(function() {
        $(this).focus();
    })
    .mouseleave(function() {
        $(this).blur();
    });

Not sure what you are expecting here but if you want something like at certain position on page if user moves mouse you want the textbox to get Focus you can do something like:

<div id='d1'>
<input type='text' id='txt' value='Search'/>
</div>

and with Jquery:

$().ready(function(){
  $('#d1').mouseover(function(){
   $(this).children().closest('Input[type=text]').focus();
  });
});

本文标签: javascriptHow to set focus to an input element (searchbox) on mouseoverStack Overflow