Changing selected DIV text

A

Andrew Poulos

I've managed to get the text a user has selected on a page (in a DIV not
a TEXTAREA) but I don't understand DOM methods well enough to change it.
That it to replace the text with some other text.

I've tried using deleteContents but I couldn't get it to work. And my
experiments with insertNode worked only intermittently.

I've read the relevant sections of developer.mozilla.org but while I
understand what the methods are supposed to do I can't actually get them
to work. Is there some info of a basic nature that explains their usage?

Andrew Poulos
 
R

Rob Mitchell

[posted and mailed]

I've managed to get the text a user has selected on a page (in a DIV not
a TEXTAREA) but I don't understand DOM methods well enough to change it.
That it to replace the text with some other text.

I've tried using deleteContents but I couldn't get it to work. And my
experiments with insertNode worked only intermittently.

I've read the relevant sections of developer.mozilla.org but while I
understand what the methods are supposed to do I can't actually get them
to work. Is there some info of a basic nature that explains their usage?

Andrew Poulos

<html>
<script>
function changeMe(divID) {
divEl = document.getElementById(divID);
if (divEl) {
divEl.innerHTML = "<strong>Now</strong> this is really cool!!!";
}
}
</script>
<body>
<div id="divMyContents">
blah blah blah<br>
<a href="javascript:changeMe('divMyContents')>Change Me</a>
</div>
</body>
</html>
 
M

Martin Honnen

Andrew said:
I've managed to get the text a user has selected on a page (in a DIV not
a TEXTAREA) but I don't understand DOM methods well enough to change it.
That it to replace the text with some other text.

I've tried using deleteContents but I couldn't get it to work. And my
experiments with insertNode worked only intermittently.

There is not necessarily some text selected that is part of only a
single text node where it would be clear what replacing the the text
means. So the question is what you wanted to replace if there is e.g. a
selection
<p><strong>Kibo|logy</strong> <em>f</em>o|r all</p>
where | is meant to show the boundary points of the range that forms the
selection. The selection as a text (i.e. window.getSelection.toString())
is 'logy fo' but you then need to decide what to do exactly when you
replace stuff.

If you want to have an example where text is replaced that is all inside
of a single text node then you can check out
<http://home.arcor.de/martin.honnen/mozillaBugs/domLevel2/test2006031702.html>
 

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,609
Members
45,253
Latest member
BlytheFant

Latest Threads

Top