phpmo Posted November 16, 2009 Share Posted November 16, 2009 <img name="7" img src="../images/italic.gif" border="0" alt="Italic"onClick="sendText(document.form2.rep, '[i] [/i]')"> I'm using the above code to insert various text fields as bbcode so that I can transfer it to html once viewed. This works but inserts the text at the end of the text. So if text already exists and I try to type above the old text, when you click them it still inserts it below the old text and not where my cursor is. Any thoughts on a way to make the text go to the cursor rather than the end of the textbox? Link to comment https://forums.phpfreaks.com/topic/181686-onclick-send-text-help/ Share on other sites More sharing options...
JustLikeIcarus Posted November 16, 2009 Share Posted November 16, 2009 From http://www.scottklarr.com/topic/425/how-to-insert-text-into-a-textarea-where-the-cursor-is/ function insertAtCaret(areaId,text) { var txtarea = document.getElementById(areaId); var scrollPos = txtarea.scrollTop; var strPos = 0; var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ? "ff" : (document.selection ? "ie" : false ) ); if (br == "ie") { txtarea.focus(); var range = document.selection.createRange(); range.moveStart ('character', -txtarea.value.length); strPos = range.text.length; } else if (br == "ff") strPos = txtarea.selectionStart; var front = (txtarea.value).substring(0,strPos); var back = (txtarea.value).substring(strPos,txtarea.value.length); txtarea.value=front+text+back; strPos = strPos + text.length; if (br == "ie") { txtarea.focus(); var range = document.selection.createRange(); range.moveStart ('character', -txtarea.value.length); range.moveStart ('character', strPos); range.moveEnd ('character', 0); range.select(); } else if (br == "ff") { txtarea.selectionStart = strPos; txtarea.selectionEnd = strPos; txtarea.focus(); } txtarea.scrollTop = scrollPos; } Usage: <textarea id="textareaid"></textarea> <a href="#" onclick="insertAtCaret('textareaid','text to insert');return false;"> Link to comment https://forums.phpfreaks.com/topic/181686-onclick-send-text-help/#findComment-958488 Share on other sites More sharing options...
phpmo Posted November 18, 2009 Author Share Posted November 18, 2009 That worked great. What if I want to load this in another window and then send it to a different page. For example how on the smilies here you can click [more] and it will open them up in a different page but it still goes to this text box. Also, any suggestions on how that select box works? Link to comment https://forums.phpfreaks.com/topic/181686-onclick-send-text-help/#findComment-959903 Share on other sites More sharing options...
phpmo Posted November 18, 2009 Author Share Posted November 18, 2009 delete Link to comment https://forums.phpfreaks.com/topic/181686-onclick-send-text-help/#findComment-959921 Share on other sites More sharing options...
phpmo Posted November 18, 2009 Author Share Posted November 18, 2009 http://test.com Link to comment https://forums.phpfreaks.com/topic/181686-onclick-send-text-help/#findComment-960032 Share on other sites More sharing options...
phpmo Posted November 19, 2009 Author Share Posted November 19, 2009 delete Link to comment https://forums.phpfreaks.com/topic/181686-onclick-send-text-help/#findComment-960721 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.