Here’s my code:
function moremagic()
{
var txt = '';
if (window.getSelection)
{
txt = window.getSelection();
}
else if (document.getSelection)
{
txt = document.getSelection();
}
else if (document.selection)
{
txt = document.selection.createRange().text;
}
else return;
if(txt=="" || txt==" "){
alert("No Text Selected");
return;}
var start = txt.anchorOffset;
var countstring = txt.toString();
alert(txt.anchorNode);
var end = txt.anchorOffset+countstring.length;
var type = prompt("Annotation Type: ");
if(type=="lp-token"){
var description = prompt("Lisp Statement: ");}
else if(type=="section-head-annotation"){
var description = "Section Head";}
else if(type=="list-item-annotation"){
var description = "list-element";}
else if(type=="sentence-annotation"){}
else {var description = prompt("Description: ");}
Arraystring = Arraystring+"#"+type+"#"+description+"#"+start+"#"+end;
alert(Arraystring);
var custom = document.getElementById("custom");
custom.value=Arraystring;
}
It generates a textnode object from text highlighted by the cursor but this function is called many different times and for each different highlight the anchorNode changes. I need the anchorNode to be a constant for all of the created textnode objects. Is there any way that the anchorNode of a textobject can be changed? Thank you!
Not totally sure what you are trying to achieve, but in reading this http://help.dottoro.com/ljkstboe.php I would have to say “No, there is no way to make the anchorNode constant” since it is based off of the selection. Now, if you always want the reference to the first character in the selected string for a particular selection (assuming the selection is only textNode), then you might be able to do this (replacing code where your
var startis):This is because if the selection is made from right-to-left rather than left-to-right, most browsers place the anchorNode at the last character and the focusNode at the first character of the selection, because the anchorNode indicates where the user started their selection.