开发者

JQuery onclick function to focus input

I want a JQuery function that means if the user clicks on some 开发者_Python百科text on the page it focuses an input.

$(document).ready(function(){
$('#header.search span').click(function(){
    $('#header.search input').focus();
});
});

That did not do anything. On Firebug nothing is registered.

Any ideas?

Marvellous


http://jsfiddle.net/HenryGarle/LXngq/

<span>
    <br>
        Span
    <br>
    <input id="InputToFocus" type="text">
</span>

$('span').click(function(){
    $('#InputToFocus').focus();
});

Seems to be working fine, It is probably a problem with your selectors. Could you post the structure of your HTML surrounding the input?


If you have id to the input element then you dont need javascript to do this. You can use for attribute with a label tag pointing to the input element id. Try this

Working demo

<label for="input1">Name</label>
<input id="input1" type="text" />

If you click on Name text the focus will be set into the input field.


$( "input" ).focus(function() {
  $( this ).next( "span" ).css( "display", "inline" ).fadeOut( 1000 );
});
span {
    display: none;
  }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p><input type="text"> <span>focus fire</span></p>
<p><input type="password"> <span>focus fire</span></p>

https://api.jquery.com/focus/


I think $('#header.search input') returns a bunch of INPUTs, not just one, so JQuery may not know which one to focus. You'll need to decide which one to focus.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜