I have pre
with contentEditable="true"
on my webpage and I am trying to make it append "\t"
when I press <TAB>
. I found some other plugins for that but they were working only on <textarea>
.
So, the problem is that when I append text to the <pre>
through jQuery, it loses the caret position. I was sure it was losing focus but it's not. So $("pre").focus()
, will do nothing.
I tried to blur it first but this is not practical cause the caret will return on different position depending on the browser. some help please... :P,
My code is here: http://jsfiddle.net/parisk/kPRpj/
firstly Try
for inserting a tab. it will insert a tab after your cursor
then in chrome
in IE8
should get you caret at the end.
(all together)
EDIT it would also be safer to add in a test to see if the lastChild is null and other such. Also in the keydown function
jQuery("pre")
would be better to bejQuery(this)
and put into a variable if you are using more than once. (which the example is but I'm too lazy)