The Hitchhiker’s Guide to Ruby On Rails Galaxy

Records of my voyage through RoR Galaxy

To make text in TextArea selectable onclick of mouse

Posted by arjunghosh on April 28, 2008

I am very sure most know about this and is a very simple thing. But as I have found sometimes that small things are the one which are missed.

So wanted a quick note on – How to make the text inside a textarea selectable on mouse click.

Well, we use the Javascript event system
[def: An event in Javascript is something that happens with or on the web page ].

Specifically we use the onclick Event to achieve the above stated goal. onClick applies to buttons (submit, reset, and button), checkboxes, radio buttons, and form upload buttons.

This is how we do it:

<textarea rows="3" cols="43" name="none" onclick="this.select();">This is the text which when clicked on will get selected</textarea>

The main syntactic sugar here is the “onclick=”this.select();”

Here the “this” helps us to access the textarea object and then we fire the DOM event “select()”
dynamically on it, which in turns selects the text inside the textarea.
ciao until next time :)

About these ads

6 Responses to “To make text in TextArea selectable onclick of mouse”

  1. A said

    Nice and simple But works great, thanks!

  2. rohan said

    thanks for sharing… i was searching this …

  3. arjunghosh said

    Nice to hear that it helped you both @A and @Rohan :)

  4. Aliakbar Yazdi said

    thanx.

  5. Nasrin said

    Thanks for sharing, it is nice post

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: