phpSensei Posted July 20, 2007 Share Posted July 20, 2007 <script type="text/javascript"> function smiley(tag) { var emo=document.forms["form"].elements["message"]; emo.value+="" } </script> the code works perfectly, but everytime you put a text into the field, it goes at the end of the string, not where the cursor is. help please. Quote Link to comment Share on other sites More sharing options...
tomfmason Posted July 21, 2007 Share Posted July 21, 2007 here is the replaceText function that this forum uses. It is not perfect but should suit your needs. function replaceText(text, textarea) { // Attempt to create a text range (IE). if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange) { var caretPos = textarea.caretPos; caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text; caretPos.select(); } // Mozilla text range replace. else if (typeof(textarea.selectionStart) != "undefined") { var begin = textarea.value.substr(0, textarea.selectionStart); var end = textarea.value.substr(textarea.selectionEnd); var scrollPos = textarea.scrollTop; textarea.value = begin + text + end; if (textarea.setSelectionRange) { textarea.focus(); textarea.setSelectionRange(begin.length + text.length, begin.length + text.length); } textarea.scrollTop = scrollPos; } // Just put it on the end. else { textarea.value += text; textarea.focus(textarea.value.length - 1); } } You can use it like this <a onclick="replaceText('your_text', document.getElementById('your_textarea'); return false;">Some Text</a> good luck Tom Quote Link to comment Share on other sites More sharing options...
phpSensei Posted July 21, 2007 Author Share Posted July 21, 2007 <a onclick="replaceText(''), document.getElementById('message'); return false;"><img src="../admin/happy.gif" alt="" width="18" height="24"></a> Am I suposed to change anything else than the ElementbyId('message') or.... Thanks for the support though.. It doesnt work because I have never used javascript so much, just pure php. 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.