with crateRange in IE and getSelection in Firefox you cannot create a selection itself but fetch the content of a selection that had been made by a user
You will not be able to make Javascript select a text on its own.
If your goal is to get the content that is described by the params you gave (obj,pos,num) then Nedals version is the way to do it.
what you could do to fake a selection is replace the content you want to highlight with "<span>content</span>" and set the css for the span in a way hat looks like a selection.
Code:
function selectText(obj,pos,num) {
var text = document.getElementById(obj).innerHTML;
beforeSelection = text.substr(0,pos);
selection = text.substr(pos,num);
afterSelection = text.substr(pos+num);
document.getElementById(obj).innerHTML = beforeSelection + "<span>" + selection + "</span>" + afterSelection;
}
if you do that you should ensure that the spans get deleted when the user clicks somewhere in the doc to avoid confusion
cu
huckepick
PS: the replacement thing only works fine if there is only plaintext in the div
If there is html to render in it it just fails.
Bookmarks