embeding text

P

Piotrek W

Hi,

Little problem...

How to write some text into current document at text coursor position?

Peter
 
Y

Yep

Piotrek W said:
How to write some text into current document at text coursor position?

You could use some ranges, see below (tested IE5.5 and Mozilla 1.3).
The example uses document.onclick for simplicity, but you could also
use some more advanced triggers (storing the mouse position for IE in
a mousemove handler, and grabbing the rangeParent/rangeOffset from the
mousemove evt object for Gecko).


document.onclick = function(evt){
var d=document;
if(d.selection && d.body && d.body.createTextRange){ //IE
var no_tags="inputtextarea";
if(d.selection.type=="Text" &&
no_tags.indexOf(event.srcElement.nodeName.toLowerCase())==-1){
var rng=d.body.createTextRange();
rng.moveToPoint(event.x, event.y);
rng.pasteHTML("Foo");
}
}else if(window.getSelection){ //Gecko
var sel=ng=window.getSelection(), rng;
if(sel.rangeCount){
rng=sel.getRangeAt(0);
if(rng.startContainer.nodeType==Node.TEXT_NODE){
var txt=d.createTextNode("Foo"),
txtAfter=rng.startContainer.splitText(rng.startOffset);
txtAfter.parentNode.insertBefore(txt, txtAfter);
sel.removeAllRanges();
}
}
}
}


HTH
Yep.
 

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
474,079
Messages
2,570,573
Members
47,205
Latest member
ElwoodDurh

Latest Threads

Top