I’ve been racking? (wrecking?) my brains trying to figure this out, and I don’t even know what search terms to use here, so here’s my question:
First off, my browser requirements are IE 5+ and Netscape 7/Firefox/Mozilla.
I have several fields on a web form that I want to keep from being edited except through a popup dialog. I’ve got the dialog code working just fine. The problem is how I keep users from entering their own data in the fields.
I don’t want to use the disabled property of the form element because… well… I just don’t like the grayed-out look.
In addition, I want the user to be able to tab through the field or click in it as if it were a normal field.
HOWEVER, if the user tabs or clicks into it and then starts to type, I want an alert that says something along the lines of “You need to use the button”.
I’ve tried the following javascript function call in the fields’ onKeyDown event with limited success:
disableKey(this, event.keyCode)
Where disableKey is:
function disableKey(fieldRef, code){
var origValue = fieldRef.value
if (code != 9){
alert('Please use the button to make changes to this field');
fieldRef.value = origValue;
} //end if
} //end function
This “sort of” works in Mozilla/Firefox, in that it allows tabs through the field and prompts the user if they try to enter text. It does allow the user to enter one character, but once they dismiss the alert, the field is restored to its prior value.
One thing I worry about is that if you keep typing while the alert is on screen, it generates some really ugly error messages in the JS console.
In IE, it works pretty much the same, EXCEPT that it doesn’t get rid of the text the user entered.
I know that Mozilla and IE handle events differently, and that’s one place where I’m hung up. I simply don’t know how to go about coding event handlers properly.
Anyone have a suggestion on how to handle this seemingly simple functionality?
IDEALLY, I would like the alert to pop up before any text appears in the field. Possible?