I have a roll your own text editor that lets you change portions of a textarea
element. i want to adapt it to work with a span element. I have no particular attachment to span. The goal is simply to let someone edit html rather than a textarea. I have it working fine in IE but am encountering some problems with Mozilla.
Since I'm using a span instead of form input I am using innerHTML
instead of value. However, I can't seem to get the selectionStart
and selectionEnd
functions to work on innerHTML
as opposed to value
.
Here is the textarea
code that works fine....
html
<textarea id="textarea>Some text goes here</textarea><a href="javascript:void() onclick="editText">edit</a>
JS
function editText() {
var len = displaytext.value.length;
var start = displaytext.selectionStart;
var end = displaytext.selectionEnd;
var sel = displaytext.value.substring(start, end); returns selection ok
alert(sel);
}
However, the following adaption is not limiting the selection to start and end.
html
<span id="textarea>Some text goes here</span><a href="javascript:void() onclick="editText">edit</a>
JS
function editText() {
var len = displaytext.innerHTML.length; //works ok
var start = displaytext.selectionStart; //does not seem to work
var end = displaytext.selectionEnd; //does not seem to work
var sel = displaytext.innerHTML.substring(start, end); //returns whole innerHTML not selection
alert(sel);
}
Is there a problem with selecionStart
on innerHTML
? Workaround? Syntax error? Thanks for any suggesions.