views:

117

answers:

2

I'm trying to make a fancy editor online, and I need to give the user the impression that he's typing directly inside a DIV / SPAN / some other element.

I can capture the keystrokes by routing them to a hidden text box, yes, but I want to know how to show a cursor at the right places to complete the illusion.

Any ideas?

A: 
#editor {cursor:text;}

(think it's "text" the cursor you want)

yoda
that changes the mouse cursor - it doesn't give you the text caret (blinking line) which I believe he's talking about.
nickf
ah .. that's different ..
yoda
he said "a cursor at the right places " so the hard part is in the right places not show the cursor
Amr ElGarhy
yeah, I'm trying to make the div act like a text box... I need to apply custom formatting and styles on the text as its being typed.
Sudhir Jonathan
+3  A: 

I believe that most of the in-browser rich text editors achieve this by using the contentEditable property (there's different names for each browser I think), so it lets the system handle that. It's a much cleaner solution since all the native commands and shortcuts (ctrl+left, double-click to select word, etc) work exactly the same as they'd expect and you don't have to do anything.

If there's a really good reason you want to go with this other way, I guess you could fudge it by using an animated gif and just shifting it with each keystroke. Unless they're using monospace font, finding where to place the cursor will be difficult...

nickf
Yeah, I thought of the cursor, but I really have no intention of going down that road.. will search for the contentEditable property now... I'll see if that's what I need.
Sudhir Jonathan
contentEditable makes your divs "really" editable, not sure if is that what you want ..
yoda
@yoda: Yes. It make it TOO editable. I actually wanted something a little less functional :-/
Sudhir Jonathan