473,468 Members | 4,558 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Return selectionStart for div?

I need to find a way to determine the character position of a users
selection in a div. I can do this with the following using an input
text box. Any way for a div or any text within the body tags?

<html>
<body>
<form>
<div onkeyup="s(this)" onclick="s(this)" id="myDiv">This is some
text</div>
<input type="text" onkeyup="s(this)" onclick="s(this)" value="This is
some text">
</form>
<script type="text/javascript">
function s(el)
{
var sel, rng, r2, i=-1;

//firefox
if(typeof el.selectionStart=="number")
{
i=el.selectionStart;
}
//ie
else if(document.selection && el.createTextRange)
{
sel=document.selection;
if(sel)
{
r2=sel.createRange();
rng=el.createTextRange();
rng.setEndPoint("EndToStart", r2);
i=rng.text.length;
}
}
else
{
el.onkeyup=null;
el.onclick=null;
}
alert(i)
}
</script>
</body>
</html>

Jul 23 '05 #1
1 18958
je*********@hotmail.com wrote:
I need to find a way to determine the character position of a users
selection in a div.


The following should demonstrate some methods you can use with ranges;
tested IE6 and Mozilla 1.7.
---
<div
onmouseup="alert(getCharPosition(this));"
onmousedown="prepare(this)">Hello, World</div>

<script type="text/javascript">
var getCharPosition=(function(){

function getSel(){
var sel=null;
if(
typeof document.selection!="undefined" &&
document.selection &&
document.selection.type=="Text"
){
sel=document.selection;
} else if(
window.getSelection &&
window.getSelection().rangeCount>0
){
sel=window.getSelection();
}
return sel;
}

function createRangeFromSel(sel){
var rng=null;
if(sel.createRange) {
rng=sel.createRange();
} else if(sel.getRangeAt) {
rng=sel.getRangeAt(0);
if(rng.toString()=="") rng=null;
}
return rng;
}

return function(el){
var sel=getSel(), rng, r2, i=-1;
if(sel){
rng=createRangeFromSel(sel);
if(rng){

if(rng.parentElement) {
if(rng.parentElement()==el){
r2=document.body.createTextRange();
r2.moveToElementText(el);
r2.collapse(true);
r2.setEndPoint("EndToStart", rng);
i=r2.text.length;
}
} else {
if(
rng.startContainer &&
rng.endContainer &&
rng.startContainer==rng.endContainer &&
rng.startContainer==rng.commonAncestorContainer &&
rng.commonAncestorContainer.parentNode==el
){

//make sure your DIV does not have any inner element,
//otherwise more code is required in order to filter
//text nodes and count chars

i=rng.startOffset;
}
}
}
}
return i;
};

})();

function prepare(el){
if(el.normalize) {
el.normalize();
}
}
</script>
---
HTH,
Yep.
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: DraguVaso | last post by:
Hi, For my VB.NET-application I want to indicate in a label the SelectionStart and the SelectionLength when the user selects some text in a Cell in a DataGrid. Does anybody knows in which...
4
by: snoopy | last post by:
Hi, Previously I have this code working in FF 0.9 in a web forum : bodytext = (document.all) ? document.selection.createRange().text:document.getSelection(); if (selectedtext.createTextRange) {...
2
by: PengYu.UT | last post by:
I have the following sample program, which can convert function object with 1 argument into function object with 2 arguments. It can also do + between function object of the same type. The last...
7
by: John S | last post by:
I have an event handler tied to a text box keypress event. All I am trying to do is have it read the key and return it in upper case. Because the event doesn't return anything, I cannot use the...
2
by: Gary Shell | last post by:
I have a very ODD situation. I have a RichTextBox and a button. In the button click event I have the following: me.richtextbox1.SelectedText="test" Assuming I start with "aaaabbbbcccc" in...
2
by: marfi95 | last post by:
Hi all, I am trying to change the color of text that the user sees when they are editing a richtextbox. This is the code I am using in the TextChanged method of RichTextBox control. Dim...
1
by: mosquito.dotnet | last post by:
I am getting clearly wrong values from TextBox.SelectionStart property in C# code. For example, if I move caret one character to right by pressing Keys.Right, sometimes declared SelectionStart...
1
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hello I've created a .NET 2.0 form and added 6 text boxes, 1 combo box and 2 button controls. The combo box has a DropDownStyle of DropDown, so the user can still type in the combo box. ...
0
nicebasic
by: nicebasic | last post by:
I have used "msdxm.ocx" to embed Windows Media Player in my VB Projects. When I decided to use "wmp.dll" to embed newer version of Windows Media Player in my projects, I noticed some differences...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.