Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 09-15-2004, 06:37 AM   PM User | #1
quadrant6
Regular Coder

 
Join Date: Aug 2002
Posts: 124
Thanks: 0
Thanked 0 Times in 0 Posts
quadrant6 is an unknown quantity at this point
Insert text at cursor position

I'm looking for code that I can use to insert a bit of text into a text box at mouse cursor position (i.e. the smilie buttons used here)

I have found a few examples and tried to use them but none so far that work across IE/Mozilla based browser (mac/pc)


Does anyone know of a script or have any suggestions?

thanks.
quadrant6 is offline   Reply With Quote
Old 09-16-2004, 11:51 AM   PM User | #2
Willy Duitt
Banned

 
Join Date: Sep 2003
Posts: 3,620
Thanks: 0
Thanked 0 Times in 0 Posts
Willy Duitt is an unknown quantity at this point
Try this: (not my code)

Code:
<script type="text/javascript">
function setCaret (textObj) {
  if (textObj.createTextRange) {
    textObj.caretPos = document.selection.createRange().duplicate();
  }
}
function insertAtCaret (textObj, textFeildValue) {
  if(document.all){  
     if (textObj.createTextRange && textObj.caretPos) {
       var caretPos = textObj.caretPos;
       caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?textFeildValue + ' ' : textFeildValue;
     }else{
       textObj.value  = textFeildValue;
     }
  }else{
    if(textObj.setSelectionRange){
      var rangeStart = textObj.selectionStart;
      var rangeEnd   = textObj.selectionEnd;
      var tempStr1 = textObj.value.substring(0,rangeStart);
      var tempStr2 = textObj.value.substring(rangeEnd);
      textObj.value = tempStr1 + textFeildValue + tempStr2;
    }else{
      alert("This version of Mozilla based browser does not support setSelectionRange");
    }
  }
}

</script>

  <form id="form1" action="" onsubmit="" method="post" enctype="text/plain"> 
  <p>
     <textarea name="tarea" rows="" cols="" style="width:300px;height:120px;"
               onselect="setCaret(this);"
               onclick="setCaret(this);"
               onkeyup="setCaret(this);" >
     Click in this textObj anywhere and then press the button below.
     It will insert the text at the caret position.</textarea>
     <br/><br/>
     <input type="text" name="textfield" style="width:220px;" value="Mozilla 1.4"/>
     <br/>
     <input type="button" value="insert at caret"
            onclick="insertAtCaret(this.form.tarea,this.form.textfield.value);"/>
    </p>
   </form>
.....Willy
Willy Duitt is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:02 AM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.