Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set focus on input element after a link is clicked

I have this clickable link,

`<a 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.

like image 917
Tsukimoto Mitsumasa Avatar asked Nov 02 '25 07:11

Tsukimoto Mitsumasa


2 Answers

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();
});
like image 175
Flo Avatar answered Nov 03 '25 20:11

Flo


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

<a href="#!" id="link">Clickable Link</a>
like image 31
lotav Avatar answered Nov 03 '25 20:11

lotav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!