Forum Moderators: open
I'm trying to disable input into a text field (myField) when a user tries to type in it. The solution cannot entail pre-setting myField as disabled, because the ability/inability to enter data into myField happens on the fly, based on other dropdown selected values, etc.
I did, however, come up with a semi-workaround for onkeypress. Here's some incomplete code, just to give an idea:
<script type = "text/javascript">
function doWhatever() {
if (document...selected.value == 0) {
if (navigator.userAgent.indexOf('Safari') >= 0) {
document.form[0].something.value = '';
document.form[0].something.disabled = true;
alert('You can't type here, blah blah blah.');
document.form[0].something.disabled = false;
}else{
... do regular onkeypress validation stuff
}
}
}
</script>
<input type = "text" name = "something" onkeypress = "doWhatever();">
At first glance, you'll ask how in the world this is the solution to my above problem.
Well, you first have to make sure you don't want to type in the input field. Then you disable the field, and throw your alert. After the user ok's the alert, Safari will try to write to the field, but since it's disabled, it will not succeed (this all happens before Safari gets to the next line of code). After Safari does its thing, and tries unsuccessfully to write to the field, you can enable the field again.
Yes, it may not make sense, but it does in fact work. Try it out. I don't pretend to really know why it works, I've long since passed trying to understand the nuances of Safari.
I don't pretend to really know why it works, I've long since passed trying to understand the nuances of Safari.
Funny, I develop in Safari and say the same thing with one change:
I don't pretend to really know why it works, I've long since passed trying to understand the nuances of Internet Explorer. :)