Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question document.body.createTextRange(), selecting...

    Hello,

    A colleague and I are trying to find a way to automatically select ONLY a specific range of text within a page, using

    document.body.createTextRange()

    and somehow referencing the id of a span... however NOTHING we have tried works. We got it to select the first thing sometimes (they may often contain the same text), and at other times it would select all spans with identical text - but we're finding it impossible to isolate the span by id and select the text just by clicking on the span. It's pretty perplexing, and this is not the type of javascript we usually mess around with... any help?

    The below obviously doesn't work, just a general example of what we're trying to do...

    Code:
    <html>
       <head>
          <title></title>
    <script type="text/javascript">
    <!--
    function textSelect(lblnumber)
    {
        alert(lblnumber);
        var range = document.???.innerText;
        range.createRange();
        range.select();
    }
    // -->
    </script>
       </head>
       <body>
          <form id="form1" action="" method="post">
          <span id="lblFasdffname" onclick="textSelect(this.id)"><*fname*></span>
          <span id="lblFasd234name" onclick="textSelect(this.id)"><*fname*></span>
          <span id="lblFasdasd3name" onclick="textSelect(this.id)"><*fname*></span>
          </form>
       </body>
    </html>
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    *sigh*

    var range = document.body.createTextRange();
    range.moveToElementText(document.all['theID']);
    range.select();


    ugghhhh, look what you made me do. icky!

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey, I try to avoid this kind of icky stuff, myself, and the "Bible" didn't help much when you're trying to figure it out quick...

    But that might help someone I'm working with, so thanks!

    I'll let you know how it goes in the morning... but I suggested we post in this forum for obvious reasons.
    Last edited by whammy; 02-21-2003 at 12:02 AM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #4
    Regular Coder
    Join Date
    Nov 2002
    Location
    Carmel California
    Posts
    471
    Thanks
    0
    Thanked 1 Time in 1 Post
    icky??????
    how is that icky??
    whats wrong with doing it that way?
    Kris Hubby
    kwhubby site

  • #5
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Because IE-only stuff is generally icky. Especially TextRanges. There is a wonderful thing called DOM2 Range, and IE has do to their own thing with TextRange. Brings a bad taste to my mouth.

    The DOM2 Range way of creating a range around some text using the same information:

    var range = document.createRange();
    range.selectNode(document.getElementById('theID'));

    Then you could surroundContents() with a styled span or something to indicate selection. (Selection is part of the browser, and not the webpage, therefore DOM doesn't deal with actual selections).

    The Gecko way of selecting text:

    var selection = window.getSelection();
    selection.removeAllRanges();
    selection.addRange(aRangeInstanceLikeTheOneWeJustCreated);

    Is only a little less icky because it provides an intelligent interface to user-selections, and uses DOM2 Range instances to describe them.

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, it works. We're also going to try to do some browser-checking so we can use the gecko method as well...
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That works just great too.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •