Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OnClick to select all text in a cell

Status
Not open for further replies.

Moonlight2

Programmer
Jun 16, 2011
1

Dear ALL

How to select all text in a cell by clicking in it ?

I would like to select all text in a cell for fast copying.

Copying 1 cell at a time.

Thanks
 
use the 'select()' method.

Code:
    document.getElementById(id).focus();
    document.getElementById(id).select();

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Very true.

if the TD had an ID can you select its content via the same method?


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Only text input elements have a select() method. Table cells and other non input elements do not. Even IE spits back an error saying the element does not support the property or method.

Sadly there's no widely supported way to select text from en element, you'll need to code conditionally for different browsers using the createRange functions for IE and other functions for other browsers.

With that said, default behavior in most browsers is to select the text in an element when its triple clicked. so triple clicking the cell will do what you need without the need of coding anything else.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
'Triple click' sounds like an RSI sufferers worst nightmare!


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top