• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Position cursor in input text element and prevent moving behind that position

 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I really just need a nudge in the right direction. Using jQuery if it matters, I have an input element and I want to pre-fill it with some text and then position the cursor after said text without selecting it. Then, I want to make sure that the cursor can't be moved behind (to the left of) the end of that text. So for example, I might pre-fill it with



The user should not be able to delete global. nor type anything except after the global. The main code I'm finding on the web is related to text selection of a range. I just need to position the cursor and prevent certain movement of it.

 
author
Posts: 297
5
Android Firefox Browser Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is more complex than it may sound. It would be far simpler to just let the user type anything then append the prefix either onsubmit or in the back end code.

If you can't do that for some reason here are some approaches to setting the caret position. As far as keeping the text there, you'll have to listen to onkeydown (or similar), detect the delete key then determine if the current caret position is inside your non-removable text.

If you only have to support modern browsers you could use a pattern attribute with a regex which requires that text to be at the start.
 
reply
    Bookmark Topic Watch Topic
  • New Topic