Results 1 to 5 of 5

Thread: textarea problem

  1. #1
    Join Date
    Aug 2009
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default textarea problem

    how can i find before/after parts of textarea? ($end & $begin)

    Code:
    function(name) {
    var input = document.selection.createRange();
    var rangeinput = input.text;
    
    var begin = rangeinput.selectionStart;
    var end = rangeinput.selectionEnd;
    
    var input = document.formname.textareaname.value = begin + '[' + name + ']' + end;
    }
    whats wrong at this code?

  2. #2
    Join Date
    Aug 2009
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    im not selecting something in textarea(as like makeing "bold/italic").. the code must show before and after parts of where the mouse cursor is.

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Do you mean where the typing cursor is? The mouse cursor is the arrow, and finding that location relative to the text box would be very difficult, if not impossible.
    Sorry I don't have an answer. I'm curious myself about the best way to approach this.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Aug 2009
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    i need begin & after parts in textarea..

    for firefox: im doing like this..
    Code:
    var begin = formname.textareaname.value.substr(0, formname.textareaname.selectionStart);
    var end = formname.textareaname.value.substr(formname.textareaname.selectionEnd);
    but, this is not working on IE.

    i need var begin & var end

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    That's not how it works in IE. There is no begin and end, well there is, but that information is difficult or impossible to work with (readonly, if I'm remembering correctly, and given as pixel offsets, not as character offsets), and difficult to obtain (the MS documentation on it is not well organized, or perhaps too organized or fragmented). You may get the range and do stuff with it. If the range length is 0, insertion will occur at the point of the cursor. As I believe I mentioned before:

    http://www.dynamicdrive.com/forums/s...3&postcount=33

    Shows the methods used. Reading the entire thread:

    http://www.dynamicdrive.com/forums/s...683#post211683

    should give you a better idea. See also:

    http://msdn.microsoft.com/en-us/libr...69(VS.85).aspx

    Which will allow you to cast about in the MS library for related tidbits on the approach used by MS for IE as regards these and other text selection situations.

    Generally what you want to do is to:

    1. Focus on the element:
      Code:
      el.focus();
    2. Get the selection and create a range from it:
      Code:
      curRng = document.selection.createRange();
    3. Empty the selection (I've no idea why, but this seems to be required):
      Code:
      document.selection.empty();
    4. Edit the text property of the range:
      Code:
      curRng.text = btag + curRng.text + etag;


    Maintaining focus on the element throughout this process, and perhaps even removing focus from it (el.blur()) and then returning focus, may be required for optimal performance. The main thing is that we never get to see or use the the begin and end point, we get the range and edit its text property. This is done in my code referenced above (here with the non-IE parts removed), like so (as part of my editText object collection of functions):

    Code:
    		saveCurRng: function(el){this.el = el; el.focus(); this.curRng = document.selection.createRange();},
    		insert: function(btag, etag){
    				this.el.focus();
    				document.selection.empty();
    				this.curRng.text = btag + this.curRng.text + etag;
    				this.el.blur();
    				this.el.focus();
    			},
    If there is no selected text, insertion (btag, etag) will occur at the point of the cursor. If there is selected text, insertion will wrap the selection.

    More on the TextRange Object created by:

    Code:
     . . . curRng = document.selection.createRange();
    and it's properties can be found here:

    http://msdn.microsoft.com/en-us/libr...72(VS.85).aspx
    Last edited by jscheuer1; 12-26-2009 at 01:36 PM. Reason: add info
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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