Revos Posted January 26, 2007 Share Posted January 26, 2007 Hi guys, 1.I am having a problem with setting focus in a specific place, I am using "focus()" how can I control where it will make the focus? I mean after how many chars..2.I have a textarea and a button, when I press the buttun the value in textarea is textarea value + "A".Now, it adds "A" only in the end, what I want is that when I will press the button it will add "A" to the place where the cursor is.For example:the value in text area is "Cool" and the cursor is here "Co|ol"[| means cursor] the value after I press the button will be "CoolA", and I want it to be "CoAol".Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/35798-putting-focus-in-textarea-in-a-specific-place/ Share on other sites More sharing options...
obsidian Posted January 26, 2007 Share Posted January 26, 2007 Is this what you're after?http://weblogs.asp.net/skillet/archive/2005/03/24/395838.aspxThis may actually be closer:http://alexking.org/blog/2003/06/02/inserting-at-the-cursor-using-javascript/ Quote Link to comment https://forums.phpfreaks.com/topic/35798-putting-focus-in-textarea-in-a-specific-place/#findComment-169700 Share on other sites More sharing options...
Revos Posted January 26, 2007 Author Share Posted January 26, 2007 Ok, for question 1 I found a half solution..works onliy in IE, how can I make it work in FF as well?this is the full code:[code]<script LANGUAGE="Javascript">var globalCursorPos; // global variabe to keep track of where the cursor was//sets the global variable to keep track of the cursor positionfunction setCursorPos() { globalCursorPos = getCursorPos(FormPost.Post);}//This function returns the index of the cursor location in//the value of the input text element//It is important to make sure that the sWeirdString variable contains//a set of characters that will not be encountered normally in your//textfunction getCursorPos(textElement) { //save off the current value to restore it later, var sOldText = textElement.value;//create a range object and save off it's text var objRange = document.selection.createRange(); var sOldRange = objRange.text;//set this string to a small string that will not normally be encountered var sWeirdString = '#%~';//insert the weirdstring where the cursor is at objRange.text = sOldRange + sWeirdString; objRange.moveStart('character', (0 - sOldRange.length - sWeirdString.length));//save off the new string with the weirdstring in it var sNewText = textElement.value;//set the actual text value back to how it was objRange.text = sOldRange;//look through the new string we saved off and find the location of//the weirdstring that was inserted and return that value 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; } }}//this function inserts the input string into the textarea//where the cursor was atfunction insertString(stringToInsert) { var firstPart = FormPost.Post.value.substring(0, globalCursorPos); var secondPart = FormPost.Post.value.substring(globalCursorPos, FormPost.Post.value.length); FormPost.Post.value = firstPart + stringToInsert + secondPart;}</SCRIPT>[/code]works only in IE =\and for question 2, no this is not what I am looking for, I am looking for something that will put the cursor and focus textarea, let me show you an example:textarea value is "Cool"I want to put the cursor and put focus here "C|ool"| means cursor, how do I do it? Quote Link to comment https://forums.phpfreaks.com/topic/35798-putting-focus-in-textarea-in-a-specific-place/#findComment-169713 Share on other sites More sharing options...
bibby Posted January 28, 2007 Share Posted January 28, 2007 great link obsidian. thx! Quote Link to comment https://forums.phpfreaks.com/topic/35798-putting-focus-in-textarea-in-a-specific-place/#findComment-170891 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.