By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,677 Members | 1,277 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,677 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.