Selecting the content of a div in a Button click.

V

visu

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
 
M

Martin Honnen

visu said:
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.
 
M

Martijn Saly

visu said:
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

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

visu

Martijn said:
So you want the plaintext-content of a div? Look at the normalize()
function.
Thanx thanx.. thanx a lot..
i really i does the thing what i want exactly..
Thanx for ur solution.

Regards,
Visu.
 
V

visu

visu said:
Thanx thanx.. thanx a lot..
i really i does the thing what i want exactly..
Thanx for ur solution.

Regards,
Visu.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,278
Latest member
BuzzDefenderpro

Latest Threads

Top