waynew Posted June 25, 2008 Share Posted June 25, 2008 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 25, 2008 Share Posted June 25, 2008 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> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.