Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Backspace KeyChar for Internet Explorer
Old 02-06-2007, 06:31 PM Backspace KeyChar for Internet Explorer
the12thplaya's Avatar
Experienced Talker

Posts: 38
Trades: 0
Hi

I have 2 small functions which are working fine in Safari and Firefox but not Internet Explorer. The functions count the characters in a text box and update a label which shows the total on each key press, including deletions.

I know that Char 8 is backspace but Internet Explorer doesn't seem to recognise it, so when you start deleting text in the box the label doesn't update until you press another key. I've tried show the key variable via alert to see what it is when backspace is pressed, but the alert window doesn't show.

Is there a way that I can get these functions to recognise the delete key in IE please? Here are the functions, and below these is the html code I'm using to call it.

Code:
function getKeyCode(e) {
	if (window.event) { return window.event.keyCode; }
	else if (e) { return e.which; }
	else { return null; }
}
Code:
function verbatimMax(e, qVerb, max) {
	if (max == 0 || max == '') { return true; }
	var key, verbLength;
	key = getKeyCode(e);
	if (key == 8) {
		verbLength = document.getElementById(qVerb).value.length - 1;
		if (verbLength < 0) { verbLength = 0; }
		document.getElementById('verbCharCount').innerHTML = 'Length: ' + verbLength + '/' + max;
		return true;
	}
	if (key == null || key == 0 || key == 9 || key == 27) {
		return true;
	}
	verbLength = document.getElementById(qVerb).value.length + 1;
	if (verbLength > max) { return false; }
	document.getElementById('verbCharCount').innerHTML = 'Length: ' + verbLength + '/' + max;
	return true;
}
This is the onkeypress bit I have on the textarea tag:
Code:
onkeypress="return verbatimMax(event, 'Q16_VERBATIM', 400)"
Thanks
the12thplaya is offline
Reply With Quote
View Public Profile Visit the12thplaya's homepage!
 
 
Register now for full access!
Old 02-07-2007, 03:22 AM Re: Backspace KeyChar for Internet Explorer
the12thplaya's Avatar
Experienced Talker

Posts: 38
Trades: 0
I found one site via google which says that some browsers don't recognise certain keys like backspace.

I found a message board though where someone wanted to disable the backspace key so that when pressed it didn't take you back to your previous page/site, so will give that a try later today:

http://www.irt.org/script/1345.htm

I also found this page which recognises backspace in Internet Explorer with a working example/script. However it's only recognised on keyup and keydown, not keypress. I guess I have to go with that:

http://www.w3.org/2002/09/tests/keys.html

Last edited by the12thplaya; 02-07-2007 at 03:41 AM..
the12thplaya is offline
Reply With Quote
View Public Profile Visit the12thplaya's homepage!
 
Old 02-07-2007, 06:43 AM Re: Backspace KeyChar for Internet Explorer
logic ali's Avatar
Super Talker

Posts: 104
Trades: 0
Quote:
Originally Posted by the12thplaya View Post
I found one site via google which says that some browsers don't recognise certain keys like backspace.

I found a message board though where someone wanted to disable the backspace key so that when pressed it didn't take you back to your previous page/site, so will give that a try later today:

http://www.irt.org/script/1345.htm

I also found this page which recognises backspace in Internet Explorer with a working example/script. However it's only recognised on keyup and keydown, not keypress. I guess I have to go with that:

http://www.w3.org/2002/09/tests/keys.html
I suggest not trying to read key values at all:
Code:
<FORM>
 <INPUT type='text' size='40' name='user' onkeyup="showCharCount(this,'lDisplay')">&nbsp;<label id='lDisplay'>&nbsp;<!--must not be empty--></LABEL>
</FORM>
<SCRIPT type='text/javascript'>
function showCharCount(field, label)
{
 document.getElementById(label).firstChild.data=field.value.length;
}
</SCRIPT>
logic ali is offline
Reply With Quote
View Public Profile
 
Old 02-07-2007, 09:39 AM Re: Backspace KeyChar for Internet Explorer
the12thplaya's Avatar
Experienced Talker

Posts: 38
Trades: 0
As simple as that? Thanks, I'll give it a bash when I get home later.

I got it working this morning by calling the same function with onkeypress and onkeydown, but if less code works I'll give it a try.
the12thplaya is offline
Reply With Quote
View Public Profile Visit the12thplaya's homepage!
 
Old 02-07-2007, 09:57 PM Re: Backspace KeyChar for Internet Explorer
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
OnKeyPress is filtered, so you won't get "special" keys. The KeyUp and KeyDown events give you more data.
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Backspace KeyChar for Internet Explorer
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.13531 seconds with 12 queries