Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > IE Selection/Range: Set range start at the click position, get char offset

Reply
Thread Tools

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

 
 
adamjroth@gmail.com
Guest
Posts: n/a
 
      04-08-2007
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

 
Reply With Quote
 
 
 
 
scripts.contact
Guest
Posts: n/a
 
      04-09-2007

http://www.velocityreviews.com/forums/(E-Mail Removed) 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>")

 
Reply With Quote
 
 
 
 
adamjroth@gmail.com
Guest
Posts: n/a
 
      04-09-2007
On Apr 8, 7:09 pm, "scripts.contact" <(E-Mail Removed)>
wrote:
> (E-Mail Removed) 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing offset-aware and offset-naive datetimes? Roy Smith Python 4 01-27-2013 03:17 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
Treeview questions - how to set set start node and how to catch click event Alan Silver ASP .Net 0 12-21-2005 10:40 AM
Translated Offset to Source Offset Lance Riedel XML 2 10-15-2003 03:04 PM



Advertisments