By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,636 Members | 1,213 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,636 IT Pros & Developers. It's quick & easy.

focus method issue

P: n/a
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 ?
Jul 14 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
pra__ wrote:
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
Jul 14 '08 #2

P: n/a
On Jul 14, 5:08*pm, Robin <a...@somewhere.comwrote:
pra__ wrote:
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
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
Jul 14 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.