473,320 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 9248
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: James Nugent | last post by:
Hi everyone, My app need to manipulate JLabels that have been added to the pane. All I need to do is simple stuff like make them disappear or change their background color. Unfortunately (and...
0
by: dawntildusk | last post by:
I am trying to get cut and paste to work from menu. Below is code I have can someone point me where I am going wrong I have the same problem with File selection to open and save using JFileChooser. ...
4
Jezternz
by: Jezternz | last post by:
Find the Caret starting and ending position in a textarea. I have been looking for a function that will do this for a long time. Its amazing how many site claim to have a function that works, but...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.