admin管理员组

文章数量:1287515

I have this clickable link,

`<a href="#" id="link">Clickable Link</a>`

and I have this <input> which will e out after the link is clicked,

    <input id="geo_title" type="text" value="some value" class="geo_title" />

in my script,

    $('#link').click(function(){

        $('input#geo_title').removeAttr('value');
        $("input#geo_title").focus();
    });

but sadly this does not focus the <input> element. What I wanted was that, after I click the link, I don't have to click the <input> element to be able to input something. What did I miss? Your help is highly appreciated. Thanks.

I have this clickable link,

`<a href="#" id="link">Clickable Link</a>`

and I have this <input> which will e out after the link is clicked,

    <input id="geo_title" type="text" value="some value" class="geo_title" />

in my script,

    $('#link').click(function(){

        $('input#geo_title').removeAttr('value');
        $("input#geo_title").focus();
    });

but sadly this does not focus the <input> element. What I wanted was that, after I click the link, I don't have to click the <input> element to be able to input something. What did I miss? Your help is highly appreciated. Thanks.

Share Improve this question asked Feb 8, 2013 at 15:20 Tsukimoto MitsumasaTsukimoto Mitsumasa 5824 gold badges20 silver badges44 bronze badges 2
  • 1 It's working in Chrome, see jsfiddle/ZgQPk In which browser do you have issues? – koopajah Commented Feb 8, 2013 at 15:22
  • Also working in firefox, be more precise... – Develoger Commented Feb 8, 2013 at 15:22
Add a ment  | 

3 Answers 3

Reset to default 4

Works fine, you'll just have to prevent the default link action, so the browser won't try to follow it.

$('#link').click(function (evt){
  $('input#geo_title').removeAttr('value');
  $("input#geo_title").focus();
  evt.preventDefault();
});

try to remove the default focus from the link by adding href=#!

<a href="#!" id="link">Clickable Link</a>

You haven't wrapped your code in

$( function() { });

or

$(document).ready( function() { });

本文标签: javascriptSet focus on input element after a link is clickedStack Overflow