Jump to content

Simple problem.


waynew

Recommended Posts

Hey guys. I have a textarea that allows the user to type a template letter. I have certain tags such as [contact] and [date] that will allow them to format dynamic information from a database. I'm not really experienced in JavaScript but I need to make it so that the user can simply click on an icon such as the smileys on these boards and the corresponding text will go into the form.

 

So for example, say I'm writing in the form, the only thing I need to do is press the link [contact] and the text "[contact] will automatically be added to the form so that I don't have to copy and paste it.

 

Any help on this or any link to a script (I've looked) would be helpful. Thanks.  :) See? I just pressed the smiley face and it did it for me automatically.

 

 

Link to comment
Share on other sites

A Goolge search for "textarea insert text" brought up this result:

 

http://www.webmasterworld.com/forum91/4686.htm

 

Here a working example with modified code from that page:

<html>
<head>

<script type="text/javascript"> 
<!-- 

function insertAtCursor(fieldID, myValue) { 

  fieldObj = document.getElementById(fieldID);

  if (document.selection)
  {
    //IE support 
    fieldObj.focus(); 
    sel = document.selection.createRange(); 
    sel.text = myValue; 
  } 
  else if (fieldObj.selectionStart || fieldObj.selectionStart == '0')
  {
    //Mozilla/Firefox/Netscape 7+ support 
    var startText = fieldObj.value.substring(0, fieldObj.selectionStart); 
    var endText = fieldObj.value.substring(fieldObj.selectionEnd, fieldObj.value.length); 
    fieldObj.value = startText + myValue + endText; 
  }
  else
  {
    //Unable to insert at current position - add to end of current value
    fieldObj.value += myValue; 
  }
} 

//--> 
</script> 



</head>
<body>

<a href="#" onclick="insertAtCursor('usertext','[contact]')">[contact]</a>
<a href="#" onclick="insertAtCursor('usertext','[date]')">[date]</a>
<br>
<textarea id="usertext"></textarea>

</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.