473,388 Members | 1,213 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,388 software developers and data experts.

IE Selection/Range: Set range start at the click position, get char offset

Hello,

I'm having trouble with IE's Range/Selection objects: when a user
click's anywhere in an element, can I create a range at that point
exactly _AND_ get the character offset? The code pasted below is a
very simple example that works in Firefox. I'd really like to know if
this can be reproduced in IE (knowing the char offset is very
important, as I need to store this information for later use).

<html>
<body>

<h1>Can this be reproduced in IE?</h1>

<div onclick="click_me( event )" style="border: 1px gray solid;
padding: 10px;">
Here is some sample text. Click anywhere in side of this div.
</div>

<script>
function click_me( event ){
var sel = window.getSelection();
var range = document.createRange();
range.setStart( sel.anchorNode, sel.anchorOffset );

var new_element = document.createElement('span');
new_element.style.color = "red";
new_element.innerHTML = '[ some text ]';

range.insertNode( new_element );

alert( "anchorOffset was: " + sel.anchorOffset );
}
</script>

</body>
</html>

Thanks in advance,
Adam

Apr 8 '07 #1
2 10498

ad*******@gmail.com wrote:
Hello,

I'm having trouble with IE's Range/Selection objects: when a user
click's anywhere in an element, can I create a range at that point
exactly _AND_ get the character offset?
I'd really like to know if
this can be reproduced in IE
var range = document.selection.createRange()
range.pasteHTML("<span style=color:red>[some text]</span>")

OR

var range=document.body.createTextRange()
range.moveToPoint(event.clientX,event.clientY)
range.pasteHTML("<span style=color:red>[some text]</span>")

Apr 9 '07 #2
On Apr 8, 7:09 pm, "scripts.contact" <scripts.cont...@gmail.com>
wrote:
adamjr...@gmail.com wrote:
Hello,
I'm having trouble with IE's Range/Selection objects: when a user
click's anywhere in an element, can I create a range at that point
exactly _AND_ get the character offset?
I'd really like to know if
this can be reproduced in IE

var range = document.selection.createRange()
range.pasteHTML("<span style=color:red>[some text]</span>")

OR

var range=document.body.createTextRange()
range.moveToPoint(event.clientX,event.clientY)
range.pasteHTML("<span style=color:red>[some text]</span>")
Thanks. I eventually went this route. While I don't get the character
offset directly this way, I can traverse the document to find the
input node (from pasteHTML(...) and determine the character position.

Apr 9 '07 #3

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

Similar topics

1
by: Neil Morris | last post by:
Hi I have the following code that lists first names of people who's surnames are 'Morris'. What I am trying to do is search for first names that don't start at the beginning of the line ie. have...
2
by: Peter Valdemar M?rch | last post by:
Hi, I'm having some trouble with IE6 and position: absolute. Try selecting some text from this page! (Macromedia template) http://morch.com/m2/prof/prof.htm Greatly simplified example:...
8
by: regrat | last post by:
It's possible create a javascript which counts all words in a webpage until the point in which I click with the mouse. Any suggests?
0
by: Sanjeev Azad | last post by:
Hi All I am using the IE Web Control in the ASP.NET project. I have added the functionality of the right click, but I am unable to capture the position of the selected node in the tree view...
1
by: samuelberthelot | last post by:
I've got the following class. the onclick event works only with IE although I believe I did proper handling for firefox. What is wrong in this script ? function PopupDiv(linkId, DivId){...
2
by: mmdst23 | last post by:
I'm working on a video player app, and I want to implement a trackbar that behaves like the one in Media Player, that is the cursor position moves to where the user clicked instead of moving in...
0
by: rdemyan | last post by:
I have a form that is displayed in datasheet mode. The form recordsource is simply Select * From.... with an Order By clause. When I right click to bring up the popup menu, the 'Filter by...
3
by: Peter Proost | last post by:
Hi group first of all I need to say that I almost never use regex hence my question may be stupid. I'm using regex to find all words that start with an @ in a string. But the regex that I figured...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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,...
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
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...

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.