Tab keycode

P

pra__

Hi,
I am developing a WYSIWYG editor using JS. I want to put in 4 spaces,
when a 'tab' key is pressed. I have this code, which does not work,
can anyone explain me about this.

<html>
<head>
<script>
function chk(e){
if (e.keyCode==9){
c = document.getElementById('code');
d = c.value ;
d = d + "\t";
// d = d+" " ;
c.innerHTML = d ;
//alert (d);
}
}
</script>
</head>

<body>
<textarea id="code" onkeydown="chk(event)" rows=20 cols=60></textarea>
</body>
</html>
 
J

Janwillem Borleffs

pra__ schreef:
I am developing a WYSIWYG editor using JS. I want to put in 4 spaces,
when a 'tab' key is pressed. I have this code, which does not work,
can anyone explain me about this.

Google for element.createTextRange

JW
 
T

Thomas 'PointedEars' Lahn

pra__ said:
I am developing a WYSIWYG editor using JS. I want to put in 4 spaces,
when a 'tab' key is pressed. I have this code, which does not work,

"Does not work" is a useless error description.
can anyone explain me about this.

<html>
<head>
<script>

Your markup is not Valid to begin with.

function chk(e){
if (e.keyCode==9){
c = document.getElementById('code');
d = c.value ;
d = d + "\t";
// d = d+" " ;
c.innerHTML = d ;

Appending the Tab character to the content of a `textarea' element will add
whitespace that is always interpreted as a single space in HTML. You should
modify the value of the `value' property instead:

c.value += "\t";

However, the Tab character is not uniformly rendered. If you want to put in
4 spaces, you should do exactly that:

c.value += " ";

And if it is your intention to insert 4 spaces at the caret position
instead, you should use your favorite search engine to look up the
corresponding FAQ entry. The suggested createTextRange() method will
only help with MSHTML, and the FAQ entry also is more verbose.


PointedEars
 
P

pra__

"Does not work" is a useless error description.



Your markup is not Valid to begin with.



Appending the Tab character to the content of a `textarea' element will add
whitespace that is always interpreted as a single space in HTML.  You should
modify the value of the `value' property instead:

  c.value += "\t";

However, the Tab character is not uniformly rendered.  If you want to put in
4 spaces, you should do exactly that:

  c.value += "    ";

And if it is your intention to insert 4 spaces at the caret position
instead, you should use your favorite search engine to look up the
corresponding FAQ entry.  The suggested createTextRange() method will
only help with MSHTML, and the FAQ entry also is more verbose.

PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
)  // Plone, register_function.js:16

Got it, Thanks :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top