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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Internet Explorer Select everything inside a div tag

    I am trying to select everything inside a <div>hello</div> tag but I cant get it to work. Is it impossible or not?
    The code looks like this:

    <script language="Javascript">
    <!--

    function HighlightAll() {
    var tempval=eval("document.all.testar")
    tempval.focus()
    tempval.select()
    }
    //-->
    </script>
    </head>
    <body>
    <div id="testar">hello</div>

    Please tell me what i've been doing wrong.......

    Tobart

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    Western US
    Posts
    169
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think select() is a method on form objects, like a text input or text area. I have used it on that, but never on a div. After some research, it seems you can use it on a TextRange object, but I'm not sure if that would help you or not.

    This might help with how the TextRange works...

    http://www.webreference.com/js/column12/textrange.html

    ShriekForth

  • #3
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you I will check it out

  • #4
    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
    var range = document.createRange();
    range.selectNode(document.getElementById('testar'));
    window.getSelection().removeAllRanges();
    window.getSelection().addRange(range);


    That is how you would do it in NS6+/any Gecko browser.

  • #5
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can you do it for Ie too?
    I will use it for ie only..

  • #6
    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
    After digging through the documentation at MSDN, this seems to work:

    var textrange = document.body.createTextRange();
    textrange.moveToElementText(document.getElementById('testar'));
    textrange.select();

    Why are you going to do this IE-only though when you have crossbrowser code? Unless this is for an intranet, it is always a good thing to be crossbrowser...

  • #7
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy IE Only

    Thank you very much and yes it is for the Intranet...
    We only have IE and it's not my choice but i have to respect the boss requests.....

  • #8
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Solved it Thank you everybody for your help

    <!--
    function HighlightAll() {
    var textrange = document.body.createTextRange();
    textrange.moveToElementText(document.getElementById('show'));
    textrange.select();

    }
    //-->


  •  

    Posting Permissions

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