supanoob Posted August 2, 2010 Share Posted August 2, 2010 ok so i have a piece of code that is used to insert text from a button when clicked, into a textarea, it all works well apart from the fact that whenever i click a second button to insert something into the field, it is added with whatever was clicked previously for example, i click a button, and then i click it would be added like so . Here is the javascript code: var globalCursorPos; function setCursorPos() { globalCursorPos = getCursorPos(postForm.post_body); } 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 = postForm.post_body.value.substring(0, globalCursorPos); var secondPart = postForm.post_body.value.substring(globalCursorPos, postForm.post_body.value.length); postForm.post_body.value = firstPart + stringToInsert + secondPart; } and here is the html with the text field: <form id="form1" name="postForm" method="post" action=""> <table width="100%" border="0" class="post_center"> <tr> <td><center> <table width="95%" border="0"> <tr> <td colspan="2" class="post"> <input TYPE="button" VALUE="BlahBlah" ONCLICK="insertString(this.value)" class="button"> <input TYPE="button" VALUE="YeaYea" ONCLICK="insertString(this.value)" class="button"> <input TYPE="button" VALUE="YupYup" ONCLICK="insertString(this.value)" class="button"></td> </tr> <tr> <td class="post" width="25%">Topic</td> <td class="post"><label> <input type="text" name="textfield" id="textfield" /> </label></td> </tr> <tr> <td class="post">Body</td> <td class="post"><label> <textarea name="post_body" id="post_body" cols="45" rows="5"></textarea> </label></td> </tr> <tr> <td colspan="2" class="post"><label> <div align="center"> <input type="submit" name="button" id="button" value="Post" /> </div> </label></td> </tr> </table> </CENTER> </td> </tr> </table> </form> Any help would be much appreciated. Quote Link to comment Share on other sites More sharing options...
supanoob Posted August 3, 2010 Author Share Posted August 3, 2010 Noone got any ideas on why? Quote Link to comment Share on other sites More sharing options...
brianlange Posted August 3, 2010 Share Posted August 3, 2010 why is the insertString function grabbing first and second parts. If you just want to append to the text area value the first and second parts are not necessary. Below I am doing a check to see if a value exists then insert a space before adding the new value to the text area. function insertString(stringToInsert) { if (postForm.post_body.value.length > 0) { postForm.post_body.value += ' ' + stringToInsert; } else { postForm.post_body.value = stringToInsert; } } 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.