Jump to content

onkeydown not firing in Opera 9


aspnetguy

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>

Link to comment
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? :)

Link to comment
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.

Link to comment
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. :)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...