473,396 Members | 1,826 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,396 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 9252
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.