PDA

View Full Version : Position of cursor in designMode?


dayfrank
06-23-2005, 05:11 PM
I'm creating a text editor in the browser.

If the user presses enter and their cursor is in the middle of a <p>, i want to insert a <br> tag. If their cursor is at the end of the <p> tag's innerHTML, then i want to insert a new <p> tag.

I can insert both tags as I need, but I can't find a way to get the position of the cursor relative to its containing tag (just assuming there's no other markup in the <p>'s innerHTML, so that selection.parentElement() will return the <p> tag).

Thanks for any wisdom. My own searching continues...

enumerator
06-23-2005, 11:23 PM
Thanks for any wisdom.
It's rude to ignore replies and start a new thread on the same topic...

dayfrank
06-24-2005, 05:32 PM
i'm sorry, when i started the second i kind of had a different question, but when I finished i was back to the same problem. i should have just edited my original post. it was a dumb mistake.

enumerator
06-24-2005, 11:55 PM
Alright, as far as I know, caret position can't be used to determine the containing element. An event (like onkeypress) at the container level can provide the tagName of event.srcElement...

IE's editor automatically handles this, however: if the [Shift] key is held down while pressing [Enter], a <br> is inserted (within the <p>).
Or, it will automatically wrap every line in a div, if one is included in the source: <div contenteditable="true"><div></div></div>

If you really want to handle this with a script, then I bet it would be much easier to use <div>s for carriage returns, and later replace those with <p> and <br> tags (where each blank line is a <div>&nbsp;</div>).

dayfrank
06-25-2005, 12:42 AM
the shift-enter is exactly what i needed to know. thanky ou very much.