Google
 

Tuesday, February 08, 2011

Javascript Deselect on Focus

Here's the scenario: you've got two text input, userid and email address. For email address field, I want to have the domain part pre-filled (say, @example.com). Now when the user enters the userid and jumps to email input, the browser selects the whole text as default behavior. But I want to append userid to the beginning and select only the userid part. Using jQuery, the usual code would be like this:

However, THIS DOESN'T WORK!!






To make this to work, you need to set the selectionStart and selectionEnd values after a while the inputbox is focused.


3 comments:

  1. very cool & good js tips for beginners, thank you very much for sharing.

    ReplyDelete
  2. I really like what you’ve posted here and wish you the best of luck with this blog! It is so interesting. Thank you for this informative read..!

    ReplyDelete
  3. Very interesting many thanks. I have found your blog innovative. You have chosen very incredible theme for your sketch. I cherished it.

    ReplyDelete