javascript - Adding autofocus attribute when input is hovered - Stack Overflow

I'm trying to add autofocus to an input form when it's hovered.Using the .appentTo attribute

I'm trying to add autofocus to an input form when it's hovered.

Using the .appentTo attribute but open to other solutions.

Here's what I have:

<input value=""/>

$(document).ready(function(){
    $("input").hover(function(){
        $("autofocus").appendTo("input");
    });
});

Fiddle: /

I'm trying to add autofocus to an input form when it's hovered.

Using the .appentTo attribute but open to other solutions.

Here's what I have:

<input value=""/>

$(document).ready(function(){
    $("input").hover(function(){
        $("autofocus").appendTo("input");
    });
});

Fiddle: https://jsfiddle/t7yj3b4n/1/

Share edited Dec 7, 2016 at 10:52 Zakaria Acharki 67.5k15 gold badges78 silver badges106 bronze badges asked Dec 7, 2016 at 10:51 Sam JefferiesSam Jefferies 5941 gold badge7 silver badges29 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

To focus an element use focus() method. Here you try to add the attribute autofocus with a bad method (take a look at Zakaria's answer) to all input. Use $(this)for target the hovered element

Example

$(document).ready(function(){
    $("input").hover(function(){
       $(this).focus();
    });
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input value=""/><input value=""/><input value=""/>

To add attribute to element you should use .attr() or .prop() and not appendTo :

$(document).ready(function(){
  $("input").hover(function(){
     $(this).prop('autofocus');
  });
});

NOTE : The autofocus attribute will not make the input focused but focus() instead.

Hope this helps.

 $(document).ready(function(){
    $("input").hover(function(){
        $(this).focus();
    });
});

https://jsfiddle/t7yj3b4n/2/

You must use .focus();

$(document).ready(function(){
     $("input").hover(function(){
        $(this).focus();
        });
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742401568a4437016.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信