focus method issue

P

pra__

Hi, i am designing a WYSIWYG editor. I want that when the user presses
a 'TAB', a count of four spaces should be put. I used this code,

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

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

However, the focus issue is that, when the user presses the 'TAB',
then the focus goes to the address bar. How should i avoid it ?
 
R

Robin

pra__ said:
Hi, i am designing a WYSIWYG editor. I want that when the user presses
a 'TAB', a count of four spaces should be put. I used this code,

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

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

However, the focus issue is that, when the user presses the 'TAB',
then the focus goes to the address bar. How should i avoid it ?

You'll probably (i.e. untested) be wanting to return false (inside the
if) so that the default TAB behaviour is ignored, and thus don't need to
c.focus.

Why do you need the variables 'c' or 'd'? Just:
document.getElementById('code').value += " ";
would replace 4 lines.

Anyway, the problem is that your code assumes that I'm typing at the end
of the textarea content. If I'm editing half way down the content and
want to insert a tab, it just sticks another at the bottom.

Why reinvent the wheel? A minute with Google reveals several other
peoples solutions to the "Tabs in TextAreas" issue.

Robin
 
P

pra__

You'll probably (i.e. untested) be wanting to return false (inside the
if) so that the default TAB behaviour is ignored, and thus don't need to
c.focus.

Why do you need the variables 'c' or 'd'? Just:
   document.getElementById('code').value += "    ";
would replace 4 lines.

Anyway, the problem is that your code assumes that I'm typing at the end
of the textarea content. If I'm editing half way down the content and
want to insert a tab, it just sticks another at the bottom.

Why reinvent the wheel? A minute with Google reveals several other
peoples solutions to the "Tabs in TextAreas" issue.

Robin

Thanks Robin,
I got the idea behind this, and also the phrase, 'reinvent the
wheel' :)
Found this useful link http://ajaxian.com/archives/handling-tabs-in-textareas

Regards,
Pranav
 

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,772
Messages
2,569,591
Members
45,100
Latest member
MelodeeFaj
Top