Ad

Set Focus On Input Element After A Link Is Clicked

- 1 answer

I have this clickable link,

`<a target="_blank" rel="nofollow noreferrer" href="#" id="link">Clickable Link</a>`

and I have this <input> which will come 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.

Ad

Answer

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();
});
Ad
source: stackoverflow.com
Ad