469,352 Members | 1,835 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

getSelectionStart on IE

I am quite new to javascript and am attempting
to write a function that will return the index
of the start of a selection, whether of not it
is "collapsed". I've got it working if its not
collapsed, and I've also got a getSelectionEnd
function working. I did this primarily by searching
the internet and finding similar stuff, so you
may recognize some of it. But I did not find
anything that specifically addresses this
problem. If this has been covered before, I
apologize as I have not seen it. My code
follows.

Thanks
Dean Hoover

function getSelectionStart(input)
{
if (input.setSelectionRange)
{
// Mozilla
return input.selectionStart;
}
else if (document.selection)
{
// IE
var textRange = document.selection.createRange().duplicate();
var pos;

if (textRange.text.length > 0)
{
// selection is not collapsed
pos = input.value.indexOf(textRange.text);
}
else
{
// selection is collapsed
pos = 0; // How do I compute this?
}

return pos;
}
else
{
return 0;
}
}

function getSelectionEnd(input)
{
if (input.setSelectionRange)
{
// Mozilla
return input.selectionEnd;
}
else if (document.selection)
{
// IE
var selectedRange = document.selection.createRange().duplicate();
selectedRange.moveStart("character", -input.value.length);
return selectedRange.text.length;
}
else
{
return 0;
}
}

Jul 23 '05 #1
1 9112
OK, I figured out a way to do it. I just do
the same thing that I do for getSelectionEnd,
if its collapsed. Does anyone know what needs
to be done to support other browsers (Opera,
for instance)?

Dean Hoover

Dean A. Hoover wrote:
I am quite new to javascript and am attempting
to write a function that will return the index
of the start of a selection, whether of not it
is "collapsed". I've got it working if its not
collapsed, and I've also got a getSelectionEnd
function working. I did this primarily by searching
the internet and finding similar stuff, so you
may recognize some of it. But I did not find
anything that specifically addresses this
problem. If this has been covered before, I
apologize as I have not seen it. My code
follows.

Thanks
Dean Hoover

function getSelectionStart(input)
{
if (input.setSelectionRange)
{
// Mozilla
return input.selectionStart;
}
else if (document.selection)
{
// IE
var textRange = document.selection.createRange().duplicate();
var pos;

if (textRange.text.length > 0)
{
// selection is not collapsed
pos = input.value.indexOf(textRange.text);
}
else
{
// selection is collapsed
pos = 0; // How do I compute this?
}

return pos;
}
else
{
return 0;
}
}

function getSelectionEnd(input)
{
if (input.setSelectionRange)
{
// Mozilla
return input.selectionEnd;
}
else if (document.selection)
{
// IE
var selectedRange = document.selection.createRange().duplicate();
selectedRange.moveStart("character", -input.value.length);
return selectedRange.text.length;
}
else
{
return 0;
}
}


Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.