I'm building a simple text editor by setting contentEditable=true
on a div (anyway i think that textarea behaves in the same way) and i have some problems with the tab key.
What i want is that when the user presses the tab key, the focus remain on the div and a tab character is added to the text.
I've solved the first part of the problem by calling preventDefault()
on the event object on keydown and now the div doesn't lose the focus, but i don't know why i can't insert the character.
The entity code for the tab char is 	
but if i try to add this string to the innerHTML Firefox replace it with a simple space (not
just a white space). I also tried with \t
but the result is the same.
So my question is, how can I insert a tab character in the text?
I know this is a bit old, but this ended up working the best for me...
Since the answer that was supposedly correct didn't work for me, I came up with this solution I think would work for more people.
That code worked for me, but also make sure to include the
white-space:pre-wrap
attribute in your css. Hope this helped!tabs and spaces are collapsed in html to one space unless in a
<pre>
tag or haswhite-space: pre
in its styleThe reason why you only see a simple space is because sequences of whitespace characters (except non-breaking spaces) are treated as a single space character inside divs. If you want the tab character to display in the way you expect you'll need to put it inside something like a
<pre>
element that renders whitespace as it is in the source HTML.you can insert a span with white-space=pre and content tab. this is better done with ranges like this :
I use this solution:
When working with editable code, a
<pre>
element does not need the last added css.keyup
vskeydown
and the position of e.preventDefault() may differ in browsers.Note: I think binding the key-event or whatever from the whole document is to be avoided. I had strange problems with Safari / iOS desktop browsers with contenteditable elements and bubbling events.