Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select All contents of a div

I'm having trouble selecting all contents of the div. Here's a demo:

http://jsfiddle.net/KcX6A/304/

It's selecting only the first line of texts, the other lines are ignored. How can I fix this?

EDIT:

By browser is google chrome

SECOND EDIT:

Too bad i'd be answering my own question. But here i've got it to work on Google Chrome:

I've replaced

selection.setBaseAndExtent(text, 0, text, 1); 

with

selection.selectAllChildren(text);

and it worked like a charm! Please guys inform me if it works with other browsers as well.

Working Demo

like image 649
Jürgen Paul Avatar asked Jan 10 '12 11:01

Jürgen Paul


2 Answers

It seems that removing webkit specific calls to setBaseAndExtent fixes issue in latest Chrome

http://jsfiddle.net/KcX6A/309/

like image 151
Aquatic Avatar answered Oct 23 '22 04:10

Aquatic


I believe you need to use innerHTML (or was it outerHTML) to get the entire text.

like image 1
Sachin Kainth Avatar answered Oct 23 '22 05:10

Sachin Kainth