Finding The Position Of The Cursor In A Text Box & Rich Formated <textarea>
Feb 16, 2006
I am writing an advanced BBCode system for my forums and I would like
to be able to find where the cursor was positioned last in the text so
I could insert the BBCode there.
Again I am writing an advanced BBCode system for my forums and I would
like to make is so that when someone puts in a [b] tag it goes bold, so
kind of a WYSIWYG editor and also for other things like [img] tags and
[url] tags. So could someone tell me how to do that. I would preferably
like it to still use the textarea tag, or at least a form component so
my existing code works.
I am currently working on a small script that allows people to insert BB-codes to edit their text. They are able to click a button (for example underline) and then the bb-code will appear at the end of the textfield-value.How can i retrieve the current position of the cursor within the textarea and then parse the bb-code within?
How to add a function that will add some text in textarea, when someone clicks on button, where the cursor is?(Like here, on daniweb textareas!)eg. There is bold button! The user clicks on it, and the text "[BOLD][/BOLD]" writes where curser/caret is. Cursor/caret (|) is focused between these two elements "[BOLD]cursor/caret (|)[/BOLD]"
I need to control the cursor position in a textbox to do this, does anyone know how to place the cursor in the textbox at a particular position
The functionality I am looking for... Excel provides data entry help based on previous entries i.e. if in a previous cell in a column I entered "Jim Bob" then the next cell I type a "J" Excel provides help by showing "Jim Bob" in a way that allows me to pick it or keep typing. This is the kind of functionality I am trying to get using JS.
In the code below I create an array of previous entries by the user over the last 45 days this all comes from a DB but that's not important here. When the user enters a key in the textbox I can pop the text box with a matching value from the array, only up to the chars entered.
The Problem or Question: Is there a way to place the curser back into the last position and highlight the remaining part of the match from the array?
This is really hard to explain but if you ever used Excel you may have an idea of what I am trying to do. Code:
I'm using contentEditable divs. I want to have a button a person can press, a modal window appears, some text from the modal window is selected, then it is to be inserted into the contentEditable div in the location the cursor was at before pressing the button. So how can I save that spot so I can insert there?
I am hoping you can help me. I am finding this problem rather complex to solve. I need to be able to find the surrounding text at the mouse position. For example, if a user clicks on a word in a paragraph, I need to programmatically know what the text is surrounding the click point. The text in question is not just content text, but DOM elements read in as text rather than the DOM element itself. For example, if a user clicks at the word "This" in the following:
<table><tr><td>This is good</td></tr> then I would like to know how to get the text "<td>" just before the "This" as text, not as a DOM element. Furthermore, how can I "enlargen" the scope of my capture, such that I can programmatically get the "<table><tr><td>" part as well as the "is good</td></tr>" part?
In IE it is a simple matter of: range.text += text; but I am having great difficulty figuring it out for FF.I have looked online for hours but the only solutions that I can find are to do with inserting text into textareas which supply you with nice 'selectionStart', 'selectionEnd' properties. Using a textarea would be useless for my purposes and a div element does not have such properties. An implementation in this case I am thinking would rely solely on the range object for the necesary selection / cursor position information, but I cannot figure out how to do this using the FF/W3C Range object.What I'm basically doing is trying to enable the user to paste unformatted (plain) text into a contenteditable zone for a WYSIWYG editor. I have got it working in IE, but FF is a whole other kettle of fish for this particular issue.
On to my problem:
I'm really not sure how to go about doing this, so any and all pointers are welcome. What I'd like to do is be able to find the position and width of a word that has been typed inside of a <textarea>, so that I might overlay some absolutely-positioned elements on top, and size them properly. I would really like to be able to do this in the most general case as possible (independent of fonts, font size, size of the textarea, etc.), but as stated any help is welcome.
However this doesn't always work in my script. Now, I've googled how to find the position of an element. And come accross many scripts which supposedly all find the position of any object. Some are very long scripts and some are very short all using a variety of methods. Incidently none of which work, they all return (0,0).
I am trying to dynamically find the position of an anchor on the page. I have a page where you can get to the anchor in the normal way (http://www.mypage.com/index.html#anchorname), but there is a script on that page that adds fields dynamically. This causes IE to lose the place where it was supposed to scroll to (via the anchor).
After the fields are added, I want to rescroll to the location of the anchor. Here is the code that I have running on the page. Code:
I have generated menus from an XML file. The XML also defines sub menus that are to open when the mouse is over any one of the menus.The menus are div tags with text in them. The location of each is based on the order that they added in and the width of the text with some padding.When the users mouse moves over the menu item it should pop up a sub menu at a relative position to the menu item.How do I get that position and width of the menu the user is over if it has never been set explicitly? Is there a way or not? If not what is the best solution for something like this?
However this doesn't always work in my script. Now, I've googled how to find the position of an element. And come accross many scripts which supposedly all find the position of any object. Some are very long scripts and some are very short all using a variety of methods. Incidently none of which work, they all return (0,0). I have a question, why would someone create a script to do what one line of code can do? Am I missing something.