SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Nov 2004
    Location
    California
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    TextArea - createTextRange, caretPos

    I know practically nothing of JavaScript, and am trying to modify a script found at Experts-Exchange for inserting BBcode into a textarea, either at cursor position or surrounding selected text. When the page first loads, and there is no cursor position, if one of the insertion buttons is pressed, I get the error 'isSelected' is undefined. (Interestingly, it seems the same thing happens on these forums!)

    Code:
    function insertTag ( txtName, tag, enclose ) { 
      var closeTag = tag; 
      if ( enclose ) { 
        var attribSplit = tag.indexOf ( ' ' ); 
        if ( tag.indexOf ( ' ' ) > -1 ) 
          closeTag = tag.substring ( 0, attribSplit ); 
      } 
      if ( isSelected ) { 
        var txtObj = eval ( "document.forms[0]." + txtName ); 
        if (txtObj.createTextRange && txtObj.caretPos) { 
          var caretPos = txtObj.caretPos; 
          caretPos.text = ( ( enclose ) ? "["+tag+"]"+caretPos.text+"[/"+closeTag+"]" : "["+tag+"]"+caretPos.text ); 
          markSelection ( txtObj ); 
          if ( txtObj.caretPos.text=='' ) { 
            isSelected=false; 
         txtObj.focus(); 
          } 
        } 
      } else { 
        // placeholder for loss of focus handler
    } 
    }
    I've tried everything I can think of to try and put focus on the textarea in that "placeholder for loss of focus handler" Else statement, but nothing seems to work. Can someone help me? Thank you!

  2. #2
    SitePoint Member
    Join Date
    Dec 2004
    Location
    Porto Alegre, RS, Brazil
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Solving the problem...

    Hi, I've had the same problem and tried some things, and one of them worked. The problem is that the variable isSelected is instaciated only by another function (which you do not pasted here) called markSelection.

    In fact, what I did is to insert a line that instantiates this variables in a global scope.

    The code seems like that:

    <script language=javascript>
    <!--

    isSelected = false;

    function markSelection ( txtObj ) {
    if ( txtObj.createTextRange ) {
    txtObj.caretPos = document.selection.createRange().duplicate();
    isSelected = true;
    }
    }

    function insertTag ( txtName, tag, enclose ) {
    if (tag != "") {
    var closeTag = tag;
    if ( enclose ) {
    var attribSplit = tag.indexOf ( ' ' );
    if ( tag.indexOf ( ' ' ) > -1 )
    closeTag = tag.substring ( 0, attribSplit );
    }
    if ( isSelected ) {
    var txtObj = eval ( "document.forms[0]." + txtName );
    if (txtObj.createTextRange && txtObj.caretPos) {
    var caretPos = txtObj.caretPos;
    caretPos.text = ( ( enclose ) ? "<"+tag+">"+caretPos.text+"</"+closeTag+">" : "<"+tag+">"+caretPos.text );
    markSelection ( txtObj );
    if ( txtObj.caretPos.text=='' ) {
    isSelected=false;
    txtObj.focus();
    }
    }
    } else {
    // placeholder for loss of focus handler
    }
    }
    }
    //-->
    </script>


    For me it worked just fine.

    Regards, Cassio Machado


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
  •