Lizah
10-20-2003, 08:54 AM
I have a javascript function (called from a button) that sets the selected text (in an editable div) to my own class "margin". The problem is that after I haved made a text "margin" and press enter all the text I type becomes "margin" too. It seems like the cursor is still inside the element. How can I set the cursor to be after the closing of <DIV class="margin"></DIV>? It only has to work in IE.
Here is my function:
function setClass(className)
{
className = "margin";
var oIFrame = document.getElementById("oDiv");
var insert;
if (oIFrame)
{
var sel = oIFrame.document.selection.createRange();
if (sel.text.length!=0)
{
if(className == "margin")
{
insert = "<DIV class="+className+">"+sel.htmlText+"</DIV>";
sel.pasteHTML(unescape(insert));
}
}
}
}
I have tried something like:
var iLength = insert.length();
var myTextRange = document.body.createTextRange();
myTextRange.moveToElementText(oIFrame);
myTextRange.move("character", iLength);
myTextRange.select();
That only seems to work with text though and I am interested in the HTML.
- Lizah
Here is my function:
function setClass(className)
{
className = "margin";
var oIFrame = document.getElementById("oDiv");
var insert;
if (oIFrame)
{
var sel = oIFrame.document.selection.createRange();
if (sel.text.length!=0)
{
if(className == "margin")
{
insert = "<DIV class="+className+">"+sel.htmlText+"</DIV>";
sel.pasteHTML(unescape(insert));
}
}
}
}
I have tried something like:
var iLength = insert.length();
var myTextRange = document.body.createTextRange();
myTextRange.moveToElementText(oIFrame);
myTextRange.move("character", iLength);
myTextRange.select();
That only seems to work with text though and I am interested in the HTML.
- Lizah