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 badges4 Answers
Reset to default 3To 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条)