Forum Moderators: open

Message Too Old, No Replies

Automatically selecting text in a form field

For easy copy and paste

         

zCat

3:23 am on Jun 6, 2006 (gmt 0)

10+ Year Member



Is it possible to use Javascript to automatically select the entire text of say an <input> field, either when the document loads or on an onclick event? And if so, any tips much appreciated.

(This is one of those cases where I'm not sure if it's possible, googling hasn't helped much - or I'm using the wrong keywords - and I'd be grateful to know whether it's a futile undertaking before I use any more time on it...)

zCat

3:43 am on Jun 6, 2006 (gmt 0)

10+ Year Member



To answer my own question: yes it is possible, problem solved :-).

Usually the solution occurs to me before I finish writing posts like the above, not this time though. Note to self: good synonym for "select" (which in a Javascript context is often associated with the <select> tag) is "highlight".

texmex

9:27 am on Jun 7, 2006 (gmt 0)

10+ Year Member



You still won't be able to do it, until you tell yourself how though :-)

zCat

12:42 pm on Jun 7, 2006 (gmt 0)

10+ Year Member



Well, once I'm sure it's doable (and I've been on many a wild geese chase barking up the wrong trees after things that sounded possible, but weren't) and have refined my googling strategy with appropriate keywords, it turns out it's just a case of setting the focus on the form field and using the JavaScript select() function (not to be confused with HTML's <select> tag) to perform the highlighting.