getSelectionStart on IE

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dean A. Hoover

    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 getSelectionSta rt(input)
    {
    if (input.setSelec tionRange)
    {
    // Mozilla
    return input.selection Start;
    }
    else if (document.selec tion)
    {
    // IE
    var textRange = document.select ion.createRange ().duplicate();
    var pos;

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

    return pos;
    }
    else
    {
    return 0;
    }
    }

    function getSelectionEnd (input)
    {
    if (input.setSelec tionRange)
    {
    // Mozilla
    return input.selection End;
    }
    else if (document.selec tion)
    {
    // IE
    var selectedRange = document.select ion.createRange ().duplicate();
    selectedRange.m oveStart("chara cter", -input.value.len gth);
    return selectedRange.t ext.length;
    }
    else
    {
    return 0;
    }
    }

  • Dean A. Hoover

    #2
    Re: getSelectionSta rt on IE

    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:[color=blue]
    > 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 getSelectionSta rt(input)
    > {
    > if (input.setSelec tionRange)
    > {
    > // Mozilla
    > return input.selection Start;
    > }
    > else if (document.selec tion)
    > {
    > // IE
    > var textRange = document.select ion.createRange ().duplicate();
    > var pos;
    >
    > if (textRange.text .length > 0)
    > {
    > // selection is not collapsed
    > pos = input.value.ind exOf(textRange. text);
    > }
    > else
    > {
    > // selection is collapsed
    > pos = 0; // How do I compute this?
    > }
    >
    > return pos;
    > }
    > else
    > {
    > return 0;
    > }
    > }
    >
    > function getSelectionEnd (input)
    > {
    > if (input.setSelec tionRange)
    > {
    > // Mozilla
    > return input.selection End;
    > }
    > else if (document.selec tion)
    > {
    > // IE
    > var selectedRange = document.select ion.createRange ().duplicate();
    > selectedRange.m oveStart("chara cter", -input.value.len gth);
    > return selectedRange.t ext.length;
    > }
    > else
    > {
    > return 0;
    > }
    > }
    >[/color]

    Comment

    Working...