Fork me on GitHub

Rails auto select text field tag

On a recent project, I needed to simulate the YouTube-ish text field selection box. You know the one where you can easily select and copy a link or embed code to send on to your friends. You click in the box, the text is automatically selected, and you can quickly copy the text.

I created a quite-simple helper for doing such things.

def auto_select_text_field_tag(name, value = nil, options = {})
  text_field_tag(name,
                 value,
                 { :onclick => "$(’#{name}’).select()",
                   :readonly => "true" }.merge(options))
end

Pretty easy when it’s all said and done. (It looks better on one line.)

Technorati Tags: ,

Share if you like:
  • Digg
  • StumbleUpon
  • del.icio.us
  • Technorati
  • Ma.gnolia
  • Reddit
  • description
Related posts:

Comments (4)

  1. thanks! wrote:

    nice!

    Monday, September 24, 2007 at 10:07 pm #
  2. Thanks for sharing this!

    One change: Use

    :onclick => “this.select()”

    instead. Shorter and perfectly valid javascript.

    Wednesday, December 19, 2007 at 5:15 pm #
  3. Tyler wrote:

    What is an auto select text field, im unsure of what this is talking about.

    Wednesday, April 8, 2009 at 1:14 am #
  4. Tyler wrote:

    ahh, nm, i read it again, i get it.

    Wednesday, April 8, 2009 at 1:15 am #