lonewolf217 Posted February 23, 2009 Share Posted February 23, 2009 I have the following code that I found online and modified slightly to fit my needs It is for entering BBCode into a textarea field and it works fine, except for the fact that every time something is entered, the focus switches to the top of the text box. Can someone help me figure out how to keep the focus at the point where the text was entered ? <script type="text/javascript"> function insert(tag1,tag2) { field = document.forms["form"].elements["desc"]; //IE SUPPORT if(document.selection) { field.focus(); sel = document.selection.createRange(); sel.text = tag1 + sel.text + tag2; } //MOZILLA/NETSCAPE SUPPORT else if (field.selectionStart || field.selectionStart == '0') { var startPos = field.selectionStart; var endPos = field.selectionEnd; field.value = field.value.substring(0, startPos) + tag1 + field.value.substring(startPos,endPos) + tag2 + field.value.substring(endPos, field.value.length); } //if nothing was selected, just insert at current cursor position else { var txt = document.forms["form"].elements["desc"]; txt.value+=tag; } } </script> Quote Link to comment Share on other sites More sharing options...
Adam Posted February 23, 2009 Share Posted February 23, 2009 Perhaps this will be of use to you.. function setCaretToEnd (input) { setSelectionRange(input, input.value.length, input.value.length); } Taken from: http://www.faqts.com/knowledge_base/view.phtml/aid/13562 There's a few other functions there that look like they may be of use as well! Adam 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.