Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Selecting the content of a div in a Button click.

175 views
Skip to first unread message

visu

unread,
Nov 29, 2006, 7:32:54 AM11/29/06
to
I need a solution to my problem.
the problem is

I ll have a button in page ... and when i click it .. content of a div

tag has to be get selected (i.e what we normally do with mouse to
selecting some part of a page).. then i can do CTRl+C to copy the
selected div content.. to paste it in MS Word or in any other external
application..


i ve tried the select + copy + paste sequence with the use of
temporary text area element to store the innerHTML of Div tag and
copied to Clipboard. but while pasting the selected content in MS word
it appears like the textual tags not html formatted.. but when u do the

same with mouse clicks and drags u get the formatted pasting in ms
word. why? is it possible to just let to user to select a div while
clicking a button, pressing ctrl+c to copy and paste the content in
another application(ms word etc)?.

Plz help me

Martin Honnen

unread,
Nov 29, 2006, 9:32:41 AM11/29/06
to
visu wrote:

> I ll have a button in page ... and when i click it .. content of a div
>
> tag has to be get selected (i.e what we normally do with mouse to
> selecting some part of a page)

<script type="text/javascript">
function selectNode (node) {
var selection, range, doc, win;
if ((doc = node.ownerDocument) && (win = doc.defaultView) && typeof
win.getSelection != 'undefined' && typeof doc.createRange != 'undefined'
&& (selection = window.getSelection()) && typeof
selection.removeAllRanges != 'undefined') {
range = doc.createRange();
range.selectNode(node);
selection.removeAllRanges();
selection.addRange(range);
}
else if (document.body && typeof document.body.createTextRange !=
'undefined' && (range = document.body.createTextRange())) {
range.moveToElementText(node);
range.select();
}
}
</script>

<div id="div1">
<h2>Example text</h2>
<p>Example paragraph. Kibology for all. All for Kibology.</p>
</div>

<div>
<input type="button" value="select div text"
onclick="window.selectNode(document.getElementById('div1'));">
</div>

works with Mozilla and IE.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Martijn Saly

unread,
Nov 29, 2006, 9:41:17 AM11/29/06
to

So you want the plaintext-content of a div? Look at the normalize()
function.

--
Martijn Saly

visu

unread,
Dec 2, 2006, 1:10:29 AM12/2/06
to
Thanx thanx.. thanx a lot..
i really i does the thing what i want exactly..
Thanx for ur solution.

Regards,
Visu.

visu

unread,
Dec 2, 2006, 1:12:23 AM12/2/06
to

sorry
i mean, it really does the thing what i want exactly..

0 new messages