36

I would like to place the cursor in an input field after a specific link is clicked. I'm using this for a small search engine.

Imagine an input field: []. Then some links which is adding a string like: Denmark, England etc. to the input field.

Now I need the cursor to be placed like this: "Denmark, [here]".

Is this possible?

* UPDATE *

I'm using this code right now to replace the text, how would you add the "position cursor"-trick with this?

$('#denmark').click(function(){     
   $('#searchBoxBig').val('Denmark, ');
});
5
  • 1
    Do you want multiple clicks to append or replace the text? Commented Oct 30, 2011 at 9:21
  • Replace the text.. I've updated the question with the text I use right now Commented Oct 30, 2011 at 9:28
  • If you also want to move the caret at the visible part of the browser, have a look at this answer: stackoverflow.com/questions/7892902/… Commented Oct 30, 2011 at 9:42
  • Looks like it's already doing that? Commented Oct 30, 2011 at 9:46
  • 5
    @Al-Mothafar Take it easy dude.. Seriously :-D Commented Nov 1, 2011 at 9:07

4 Answers 4

54

yes, catch the click event on the anchor prevent the default action and focus on the field.

the function context contains the element that has been clicked so u can compute which field to apply the focus call on.

$(anchorSelector).live("click", function(e) {
   e.preventDefault(); 

   $(fieldSelector).focus();

   return false;
});
Sign up to request clarification or add additional context in comments.

4 Comments

I've updated the question, how would you add the trick to my current code, to replace the text?
This is the way to go!
Practically and with a new update from Jquery we should go like this $(document).on("click","#go_to_comments", function(e) { e.preventDefault(); $("#commentaire").focus() return false; })
$(fieldSelector).focus(); is not working
30

You can set where the cursor is in an input box with the selectionStart and selectionEnd properties. If you don't want any text to be selected, just make them equal to each other.

Here's how you might do it:

var input = $("input");
input[0].selectionStart = input[0].selectionEnd = input.val().length;

1 Comment

this did not work on mobile safari for me, had to use the answer of Nicolas Modrzyk.
7
$(":input[name=xxxx]").focus();

Ah, you need the cursor to be after all the text? Take a look here: jQuery Set Cursor Position in Text Area it works the same for an input instead of a textarea.

This also works if you use other selectors.

Comments

1

You can use this :

jQuery('.className').focus();

Just use your input filed class name

2 Comments

What happens if multiple fields use the same class? This should be an ID instead, to ensure that the correct field gets selected.
we can select with parent class like- jQuery('.parentClass .className').focus();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.