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: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • StumbleUpon
  • del.icio.us
  • Technorati
  • Ma.gnolia
  • Reddit
  • DZone

Comments (2)

  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 #