473,406 Members | 2,336 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,406 software developers and data experts.

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 18954
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...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.