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

Discussion in 'Javascript' started by adamjroth@gmail.com, Apr 8, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. 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>")
     
    scripts.contact, Apr 9, 2007
    #2
    1. Advertising

  3. Guest

    On Apr 8, 7:09 pm, "scripts.contact" <>
    wrote:
    > 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, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lance Riedel

    Translated Offset to Source Offset

    Lance Riedel, Oct 14, 2003, in forum: XML
    Replies:
    2
    Views:
    502
    Patrick TJ McPhee
    Oct 15, 2003
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,058
    Ian Collins
    May 9, 2006
  3. Loebb
    Replies:
    0
    Views:
    203
    Loebb
    Feb 23, 2004
  4. Replies:
    1
    Views:
    158
  5. Roy Smith
    Replies:
    4
    Views:
    276
    Roy Smith
    Jan 27, 2013
Loading...

Share This Page