Jump to content
Sign in to follow this  
aspnetguy

onkeydown not firing in Opera 9

Recommended Posts

I have the following script that captures the TAB keypress and inserts a tab into the textarea. It works fine in IE and Firefox but the event isn't even firing in Opera 9. I am stumped.

<html><head>	<title>tabs in textarea</title>	<script type="text/javascript">			function keypressed(e)		{			var key = null;			if(window.event) key = event.keyCode;			else if(e.which) key = e.which;			if(key != null && key == 9)			{				//IE				if(document.selection)					{					this.focus();					var sel = document.selection.createRange();					sel.text = "\t";				}				//Mozilla + Netscape				else if(this.selectionStart || this.selectionStart == "0")				{					var start = this.selectionStart;					var end = this.selectionEnd;					this.value = this.value.substring(0,start) + "\t" + this.value.substring(end,this.value.length);				}				else this.value += "\t";					return false;			}		}		</script></head><body>	<textarea id="ta" rows="10" cols="50"></textarea>	<script type="text/javascript">		document.getElementById("ta").onkeydown = keypressed;	</script>	</body></html>

Share this post


Link to post
Share on other sites

I loaded up your code in the Tryit Editor in Opera 9.10 and it ran just fine. When I typed in some text and hit the tab key, a tab was inserted into the textarea.I've never tried before, does it really take that much code to insert a tab into a textarea? :)

Share this post


Link to post
Share on other sites

lol yes it does. If it weren't for the cross browser code it coudl be reduced to about 7 lines.Maybe I have a caching problem in Opera on my PC. I'll check it again....okay I cleared out my cache and upgraded to 9.2 and it is still not firing but I did run it in the Try it editor and works, I guess that is all that matters. Must be a problem on my end.

Share this post


Link to post
Share on other sites

I remember seeing somewhere that someone just turned the content editing mode on of the textarea and it would tab in normally. Much shorter. Maybe it was jquery? Loading...http://interface.eyecon.ro/demos/ttabs.html#Yup. There ya go.And this is the source code for the page:

<html><head><title>TTabs demo - Interface plugin for jQuery</title>		<script type="text/javascript" src="/jquery/jquery.js"></script>		<script type="text/javascript" src="/interface/interface.js"></script></head><body><textarea id="testArea" cols="50" rows="10"></textarea><a href="#" onClick="$('#testArea').EnableTabs()">enable tabs in textarea</a> <a href="#" onClick="$('#testArea').DisableTabs()">disable tabs in textarea</a></body></html>

The interface plugin & jquery. :)

Share this post


Link to post
Share on other sites

but I don't want content editing on I only want tabs to work, it is for a control panel code editor (no WYSIWYG)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...