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

A

adamjroth

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
 
S

scripts.contact

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>")
 
A

adamjroth

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top