faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

107 of 205 people (52%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

Insert string onButtonClick at cursor position in textarea and not at the end of string

Apr 6th, 2008 19:29
ha mo, senthil kumar, nilesh shah,


Please Use the InsertText Function
  The First argument is the textbox/textarea control, the next is the 
Text to be inserted...
<script>
function InsertText(input, insTexte)
{
startTag = '';
endTag = '';
     if (input.createTextRange)
     {
      var text;
      input.focus(input.caretPos);
      input.caretPos = document.selection.createRange().duplicate();
      if(input.caretPos.text.length>0)
      {
       input.caretPos.text = startTag + input.caretPos.text + endTag;
      }
      else
      {
       input.caretPos.text = startTag + " " + insTexte + " " + endTag;
      }
     }
     else input.value += startTag + insTexte + endTag;
}
</script>
<html>
<form name=frm>
<textarea name="visor" cols="50" rows="4" wrap=virtual></textarea>
<br>
<input type=button onClick="javascript:InsertText
(document.frm.visor,'senthil');">
</form>
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com