select html within a div

P

pbeisel

i would like to select the HTML within a DIV when a user clicks in it.
then the user can ctrl-c and get the HTML on the clipboard (much as if
the user drag-selected and area of the page and then hit ctrl-c).

the problem with the drag to select method is that its imprecise or
hard for the user to control. if i could simply select the text for
him, then all he needs to do i click and ctrl-c.

for a standard textbox i simply have an onclick handler set to
'this.select();'. all the user has to do is click on the textbox
(which is a non-editable, meaning read-only) and the text is selected
and then hit ctrl-c and onto the clipboard it goes...

of course, this doesn't work for the DIV.

any ideas?
 
B

Bart Van der Donck

i would like to select the HTML within a DIV when a user clicks in it.
then the user can ctrl-c and get the HTML on the clipboard (much as if
the user drag-selected and area of the page and then hit ctrl-c).

the problem with the drag to select method is that its imprecise or
hard for the user to control. if i could simply select the text for
him, then all he needs to do i click and ctrl-c.

for a standard textbox i simply have an onclick handler set to
'this.select();'. all the user has to do is click on the textbox
(which is a non-editable, meaning read-only) and the text is selected
and then hit ctrl-c and onto the clipboard it goes...

of course, this doesn't work for the DIV.

any ideas?

One workaround:

<form>
<div id="D">a<hr>b<font color="red">c</font></div>
<input type="button" value="Get HTML from div" onClick="doSelect()">
<br>
<textarea name="E" cols="30" rows="10"></textarea>
</form>
<script type="text/javascript">
function doSelect() {
document.forms[0].E.value = document.getElementById('D').innerHTML
document.forms[0].E.select()
}
</script>
 

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

Forum statistics

Threads
473,790
Messages
2,569,637
Members
45,346
Latest member
EstebanCoa

Latest Threads

Top