CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2006
    Posts
    7

    Question Can I manually set selection in document

    Can I manually set selection for some part of document?
    ...in other words somethin' like 'setSelection()' opposite for 'document.getSelection()' method.

    In DOM Level 2 specification i find Range object:
    myRange = document.createRange();
    This object has some methods includin' myRange.setStart(), myRange.setEnd(), which allow manualy set selection.
    This object is supported by Opera and FF BUT!!! doesn't supported by IE.
    Instead IE has document.selection.createRange() and
    document.body.createTextRange();
    But how I can set selection in IE??? Is it possible?

    TIA
    Last edited by =vd=; August 2nd, 2006 at 02:23 PM.

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: Can I manually set selection in document

    First off, getSelection() is a large compatibility problem. Take a look at the chart on this page.

    Second, for browsers compatibility you will only be able to select all text within an HTML DOM object.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Aug 2006
    Posts
    7

    Question Re: Can I manually set selection in document

    Quote Originally Posted by peejavery
    Second, for browsers compatibility you will only be able to select all text within an HTML DOM object.
    How i can do it? In FF and Opera it's ok, but IE: how i can do it in IE?

    THX

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: Can I manually set selection in document

    I already posted the solution. Look at that webpage. Hightlight a selection and click the "Get selection" button. Use getSelection().

    Someone else has already solved it for you. It would be a waste of time for me to rewrite that webpage.


    Please do not go and edit a post 4 hours after you have already posted. It only succeeds in confusing people. Remember that people will read through this and everything should be in chronological order but will not be if you edit a post 4.5 hours later and after someone else posted after you.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Aug 2006
    Posts
    7

    Exclamation Re: Can I manually set selection in document

    Quote Originally Posted by peejavery
    Please do not go and edit a post 4 hours after you have already posted. It only succeeds in confusing people. Remember that people will read through this and everything should be in chronological order but will not be if you edit a post 4.5 hours later and after someone else posted after you.
    Sorry for editin' my 1'st post. I make apology to u ...


    Quote Originally Posted by peejavery
    I already posted the solution. Look at that webpage. Hightlight a selection and click the "Get selection" button. Use getSelection().
    But I need to SET selection on page, not GET it.
    I wanna to select some word on the page, (word with wrong spelling for ex.)
    I know how do it in FF, but don't know how do it in IE.

    THX

  6. #6
    Join Date
    May 2002
    Posts
    10,943

    Re: Can I manually set selection in document

    Okay, I am following you now. Below is some code to help you out.

    Code:
    <html>
    <body>
    
    <textarea id="ta" cols=80 rows=10>Test.</textarea>
    
    <script language="JavaScript">
    var amount = 4;
    var d = document.getElementById('ta');
    var oRange = d.createTextRange();
    oRange.moveStart("character", 0);
    oRange.moveEnd("character", amount - d.value.length);
    oRange.select();
    d.focus();
    </script>
    
    </body>
    </html>
    Last edited by peejavery; August 2nd, 2006 at 05:19 PM.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  7. #7
    Join Date
    Aug 2006
    Posts
    7

    Re: Can I manually set selection in document

    This code is the 1st realy working thing for IE, I find, thx.

    ... But Range object in IE can be created only for <input> or <textarea>
    And if I need to select text in <div>,<p> or another DOM node - method createTextRange() doesn't work.

    Probably it's impossible

    Anyway BIG THX 4U.

  8. #8
    Join Date
    May 2002
    Posts
    10,943

    Re: Can I manually set selection in document

    That is what I was telling you with browser incompatibility. IE is a poor attempt by Microsoft at a web browser. IE7 BETA is pretty good but I use Safari at home and Firefox at work.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured