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 4 of 4
  1. #1
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts

    Add text at a certain location inside an input

    Hey, I'd like to make a function which adds a certain text to a certain place inside my input. ie: this is my input:
    ["Hello() This is my input"]
    and I'd like to insert "world" inside the parenthesis.
    How can I do it?

    And if I want to do the following without even knowing the place I want it to go, and just use the function to determine the place that was last focused on the input, how can I do that?

    I know it's possible since I've used this code in the past but, unfortunetly, I can't find it and I've tried googling this but had no clue how this function is called.

    Thanks for any help!

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,513
    Thanks
    3
    Thanked 504 Times in 491 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Untitled</title>
    <script language="JavaScript" type="text/javascript">
    <!--
    
    function checkCursor(zxcid,zxctag){
     var zxcobj=document.getElementById(zxcid)
     var zxcbegin=0;
     if (document.selection){
      zxcobj.focus();
      var zxcsel=document.selection.createRange();
      if(zxcsel.text==''){
       zxcsel.text='µµµ';
       dummy=zxcobj.createTextRange();
       dummy.findText('µµµ');
       dummy.select();
       zxcbegin=zxcobj.value.indexOf('µµµ');
       document.selection.clear();
       zxcAddTag(zxcobj,zxcbegin,zxcbegin,zxctag)
       zxcobj.focus();
      }
      if(zxcsel.text!=''){
       var zxclen=zxcsel.text.length
       zxcsel.text='µµµ'+zxcsel.text;
       zxcdummy=zxcobj.createTextRange();
       zxcdummy.findText('µµµ');
       zxcdummy.select();
       zxcdummy.scrollIntoView();
       zxcbegin=zxcobj.value.indexOf('µµµ');
       document.selection.clear();
       if(zxcbegin+1==zxcbegin+zxclen){ zxcAddTag(zxcobj,zxcbegin,zxcbegin,zxctag); }
       else{ zxcAddTag(zxcobj,zxcbegin,zxcbegin+zxclen,zxctag); }
      }
     }
     else if (zxcobj.selectionStart||zxcobj.selectionStart=='0') {
      zxcbegin=zxcobj.selectionStart;
      var zxcend=zxcobj.selectionEnd;
      if(zxcend==zxcbegin){ zxcAddTag(zxcobj,(zxcbegin),zxcbegin,zxctag); }
      if(zxcend!=zxcbegin){
       if(zxcend==zxcbegin+1){ zxcAddTag(zxcobj,zxcbegin,zxcbegin,zxctag); }
       else{ zxcAddTag(zxcobj,zxcbegin,zxcend,zxctag); }
      }
     }
    }
    
    function zxcAddTag(zxcobj,zxcsrt,zxcend,zxctag){
     var zxcval=zxcobj.value;
     zxctxt=zxcval.substring(zxcsrt,zxcend);
     zxcsrt=zxcval.substring(0,zxcsrt);
     zxcend=zxcval.substring(zxcend);
     zxcobj.value=zxcsrt+zxctag+zxctxt+zxctag.replace('<','</')+zxcend;
    }
    
    //-->
    </script>
    
    </head>
    <body>
    <input id="txt" value="abcdefgdefghijklmno"  ><br>
    <input type="button" name="" value="<p> Tag" onclick="checkCursor('txt','<p>');">
    </body>
    </html>
    Code:
    var myvar='fred';
    inputobj.value=inputobj.value.replace('()','('+myvar+')');
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    Hey, Thanks for the reply!
    The code doesn't work so good because it adds <p> twice (closing it as well in both cases) and I don't want it to surround the whole text, just the part that is selected.
    Also, I wanted it to find the cursor location.
    Btw, you don't have to write the whole code, just the functions (the javascript commands as in createTextRange) that I could use.

    Thanks again philip!
    Last edited by BarrMan; 02-23-2007 at 03:11 PM.

  • #4
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    bump.


  •  

    Posting Permissions

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