TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 03-23-2008, 01:13 AM   #1 (permalink)
The Addict
 
Join Date: Nov 2007
Posts: 264
Thanks: 2
TlcAndres is on a distinguished road
Default tab key emulation

Basically what I'd like to achieve is when a user hits the tab key within a textarea it actually tabs out, I'm not much for javascript and I've tried researching and only came up with looking out for the key event 9 using windows.event or the netscape equivalent.
__________________
"What everyone seems to forget is that while knowledge certainly is something - it's the implementation of knowledge that brings power" - Andres Galindo.
TlcAndres is offline  
Reply With Quote
Old 03-23-2008, 02:10 AM   #2 (permalink)
The Contributor
 
DeMo's Avatar
 
Join Date: Jan 2008
Location: Brazil
Posts: 77
Thanks: 14
DeMo is on a distinguished road
Default

Hmmmm maybe you could insert a certain number of spaces when the user presses TAB?

Anyway, I think it's a little annoying too.
Everytime I'm writing a piece of code here in the forums I always press TAB to try to indent the code.
Send a message via ICQ to DeMo Send a message via MSN to DeMo Send a message via Skype™ to DeMo
DeMo is offline  
Reply With Quote
Old 03-23-2008, 11:55 AM   #3 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

You basically need to overwrite the default command for the TAB key inside that textarea only (e.g. if the textarea is focused). I think you can easily insert a \t and a tab space will appear (it works with newlines, so it *should* work with tabs, as well). It's better to test first, though, just in case
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 03-23-2008, 01:26 PM   #4 (permalink)
The Wanderer
 
Join Date: Jan 2008
Posts: 24
Thanks: 13
sidisinsane is on a distinguished road
Default

Hi Andres,
this script I found a while ago should help. It's not very nice code and it's also not unobtrusive, but it should work with most browsers.

Code:
<html>

<head>

<script type="text/javascript">
/* 
A function to capture a tab keypress in a textarea and insert 4 spaces and NOT change focus originally by Greg Pinero.
Source: http://www.answermysearches.com/here-is-how-to-make-the-tab-work-in-a-textarea-javascript/265/
*/

function enable_tab(e,el)
{
	// 9 is the tab key, except maybe it's 25 in Safari? oh well for them ...
    if(e.keyCode==9)
    {
		// So the scroll won't move after a tabbing
		var oldscroll = el.scrollTop;
		// This doesn't seem to help anything, maybe it helps for IE
        e.returnValue=false;
        // Check if we're in a firefox deal
		if (el.setSelectionRange)
		{
			var pos_to_leave_caret=el.selectionStart+4;
			// Put in the tab
			el.value = el.value.substring(0,el.selectionStart) + '    ' + el.value.substring(el.selectionEnd,el.value.length);
            // There's no easy way to have the focus stay in the textarea, below seems to work though
			setTimeout("var t=document.getElementById('tab'); t.focus(); t.setSelectionRange(" + pos_to_leave_caret + ", " + pos_to_leave_caret + ");", 0);
		}else{
			// IE code, pretty simple really
			document.selection.createRange().text='    ';
		}
		// Put back the scroll
        el.scrollTop = oldscroll;
	}
}
</script>

</head>


<body>

<textarea name="tab" id="tab" onkeydown="enable_tab(event,document.getElementById('tab'))"></textarea>

</body>

</html>
sidisinsane is offline  
Reply With Quote
The Following User Says Thank You to sidisinsane For This Useful Post:
ETbyrne (03-25-2008)
Old 03-25-2008, 01:58 AM   #5 (permalink)
how quixotic are you?
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
ETbyrne is on a distinguished road
Default

That's a good find you have there sidisinsane, the only problem is it adds spaces, not tabs. You can easily fix this though by switching out a few lines:

Code:
<html>

<head>

<script type="text/javascript">
/* 
A function to capture a tab keypress in a textarea and insert 4 spaces and NOT change focus originally by Greg Pinero.
Source: http://www.answermysearches.com/here-is-how-to-make-the-tab-work-in-a-textarea-javascript/265/
*/

function enable_tab(e,el)
{
	// 9 is the tab key, except maybe it's 25 in Safari? oh well for them ...
    if(e.keyCode==9)
    {
		// So the scroll won't move after a tabbing
		var oldscroll = el.scrollTop;
		// This doesn't seem to help anything, maybe it helps for IE
        e.returnValue=false;
        // Check if we're in a firefox deal
		if (el.setSelectionRange)
		{
			var pos_to_leave_caret=el.selectionStart+4;
			// Put in the tab
			el.value = el.value.substring(0,el.selectionStart) + "\t" + el.value.substring(el.selectionEnd,el.value.length);
            // There's no easy way to have the focus stay in the textarea, below seems to work though
			setTimeout("var t=document.getElementById('tab'); t.focus(); t.setSelectionRange(" + pos_to_leave_caret + ", " + pos_to_leave_caret + ");", 0);
		}else{
			// IE code, pretty simple really
			document.selection.createRange().text="\t";
		}
		// Put back the scroll
        el.scrollTop = oldscroll;
	}
}
</script>

</head>


<body>

<textarea name="tab" id="tab" onkeydown="enable_tab(event,document.getElementById('tab'))"></textarea>

</body>

</html>
All I did was change the ' ' things to "\t"
__________________
Dingo Web Systems > http://www.dingocode.com
My Website > http://www.evanbot.com
ETbyrne is offline  
Reply With Quote
The Following User Says Thank You to ETbyrne For This Useful Post:
sidisinsane (04-08-2008)
Old 03-25-2008, 04:13 PM   #6 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default

I say its nicer with the JQuery manipulation of the regular keycodes ;)
__________________
PHP/XHTML Freelancer:
Cleanscript.com v3 - Programming starting at just $5 act now!
Nor is offline  
Reply With Quote
Old 03-25-2008, 10:46 PM   #7 (permalink)
how quixotic are you?
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
ETbyrne is on a distinguished road
Default

True, but the minified version of jquery.js is over 58 KB. This is only 1.5 KB including all of the HTML. It really depends on if you are doing just this or if you are also using jquery for something else on the same page (and or site).
__________________
Dingo Web Systems > http://www.dingocode.com
My Website > http://www.evanbot.com
ETbyrne is offline  
Reply With Quote
Old 03-26-2008, 08:10 PM   #8 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default

Very True ;)
__________________
PHP/XHTML Freelancer:
Cleanscript.com v3 - Programming starting at just $5 act now!
Nor is offline  
Reply With Quote
Old 03-26-2008, 09:25 PM   #9 (permalink)
The Addict
 
Join Date: Nov 2007
Posts: 264
Thanks: 2
TlcAndres is on a distinguished road
Default

Forgot to mention...I took the above script and modified (it was creating tabs and kept on going to the next button). Works a charm now - thanks!
__________________
"What everyone seems to forget is that while knowledge certainly is something - it's the implementation of knowledge that brings power" - Andres Galindo.
TlcAndres is offline  
Reply With Quote
Old 04-08-2008, 06:39 AM   #10 (permalink)
The Wanderer
 
Join Date: Jan 2008
Posts: 24
Thanks: 13
sidisinsane is on a distinguished road
Default

Quote:
Originally Posted by TlcAndres View Post
Forgot to mention...I took the above script and modified (it was creating tabs and kept on going to the next button). Works a charm now - thanks!
Can you post your modification?
sidisinsane is offline  
Reply With Quote
Old 04-11-2008, 08:30 PM   #11 (permalink)
The Addict
 
Join Date: Nov 2007
Posts: 264
Thanks: 2
TlcAndres is on a distinguished road
Default

Quote:
function stoptab(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
var el = document.getElementById('css_text');
if ((evt.keyCode == 9)) {
var oldscroll = el.scrollTop;
evt.returnValue=false;
if (el.setSelectionRange) {
var pos_to_leave_caret=el.selectionStart+4;
el.value = el.value.substring(0,el.selectionStart) + '\t' + el.value.substring(el.selectionEnd,el.value.length );
}else{
document.selection.createRange().text='\t';
}
el.scrollTop = oldscroll;
return false;
}

}

document.onkeypress = stoptab;
The only downside is it works when ever it presses tab but thats the only version, the new one just has the addons to see if it's focused on the textbox
__________________
"What everyone seems to forget is that while knowledge certainly is something - it's the implementation of knowledge that brings power" - Andres Galindo.
TlcAndres is offline  
Reply With Quote
The Following User Says Thank You to TlcAndres For This Useful Post:
sidisinsane (10-31-2008)
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 05:30 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design