jaymc Posted October 15, 2006 Share Posted October 15, 2006 Right, I am making a PM system, when sending a message users can click on an emoticon and have the emoticon code inserted in the message boxThat works fine, see below for the code im currently using[code]<a OnClick="document.form.message.value = document.form.message.value + ' :boxing: '";><img src=default/boxing.gif border=0></a>[/code]However, On clicking the emotion image the code is being inserted at the end of the text in the message boxThis still works, but, if someone has typed 3 lines out and decides the want an emoticon on line 2, on clicking the emoticon it is inserted at the end (line 3)How can I get it to insert the emoticon code where the focus of the cursor is currently at?Thanks Quote Link to comment Share on other sites More sharing options...
fenway Posted October 15, 2006 Share Posted October 15, 2006 There are multiple threads on these boards that discuss how to find the currrent caret position... search for them. Quote Link to comment Share on other sites More sharing options...
jaymc Posted October 15, 2006 Author Share Posted October 15, 2006 I dont know what to search for thoughCaret postion?I searched for that and it gave no matches Quote Link to comment Share on other sites More sharing options...
fenway Posted October 15, 2006 Share Posted October 15, 2006 I don't know either... something with TEXTAREAs and cursor positions, I think. If I can find the thread(s) again, I'll provide you with the link. Quote Link to comment Share on other sites More sharing options...
jaymc Posted October 16, 2006 Author Share Posted October 16, 2006 Ive found this code but im unsure how to use it, im not too good with javascript, this is above my level[code]function insertAtCursor(myField, myValue) {//IE supportif (document.selection) {myField.focus();sel = document.selection.createRange();sel.text = myValue;}//MOZILLA/NETSCAPE supportelse if (myField.selectionStart || myField.selectionStart == ‘0′) {var startPos = myField.selectionStart;var endPos = myField.selectionEnd;myField.value = myField.value.substring(0, startPos)+ myValue+ myField.value.substring(endPos, myField.value.length);} else {myField.value += myValue;}}// calling the functioninsertAtCursor(document.formName.fieldName, ‘this value’);[/code]I assume I would call it like this[b]OnClick="insertAtCursor(myField, myValue)"[/b]What should MyField and myValue be?Also, do i need to change the properties of this line[b]// calling the functioninsertAtCursor(document.formName.fieldName, ‘this value’);[/b]fornName = manually enter the name of my form etc? Quote Link to comment Share on other sites More sharing options...
jaymc Posted October 16, 2006 Author Share Posted October 16, 2006 This works in IE perfect, but in firefox, it inserts the value at the begining of the text area, god knows why. Anyway, here is the code, any ideas why?Or perhaps another script I can use?This works :)[code] <script LANGUAGE=\"Javascript\"> var globalCursorPos; // global variabe to keep track of where the cursor was function setCursorPos() { globalCursorPos = getCursorPos(form.message); } function getCursorPos(textElement) { var sOldText = textElement.value; var objRange = document.selection.createRange(); var sOldRange = objRange.text; var sWeirdString = '#%~'; objRange.text = sOldRange + sWeirdString; objRange.moveStart('character', (0 - sOldRange.length - sWeirdString.length)); var sNewText = textElement.value; objRange.text = sOldRange; for (i=0; i <= sNewText.length; i++) { var sTemp = sNewText.substring(i, i + sWeirdString.length); if (sTemp == sWeirdString) { var cursorPos = (i - sOldRange.length); return cursorPos; } } } function insertString(stringToInsert) { var firstPart = form.message.value.substring(0, globalCursorPos); var secondPart = form.message.value.substring(globalCursorPos, form.message.value.length); form.message.value = firstPart + stringToInsert + secondPart; } </SCRIPT>[/code] Quote Link to comment Share on other sites More sharing options...
fenway Posted October 16, 2006 Share Posted October 16, 2006 Like I said, I'm not familiar with these scripts, so I can't really help much... I don't know the intricacies of how FF handles this. 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.