View Full Version : javascript, focus and firefox
G Funk
05-23-2004, 02:20 PM
I need to force valid entry into a field, so I don't want the user to be able to leave the field until the entry is valid.
Right now, I am validating the entry in the onBlur event, and setting focus back to the field if it's invalid by calling the field's focus().
It seems to work in IE, but Firefox doesn't reset the focus, yet it will set focus to fields other than the one calling the onBlur fine.
Any ideas?
agent002
05-23-2004, 02:28 PM
It's a security feature; if you try to blur a field that focuses itself automatically, you can't focus the address bar of your browser. And a web page shouldn't be able to take control over your address bar.
G Funk
05-23-2004, 03:07 PM
Well that makes sense.
Unrelated question:
Inside an onBlur event, can you tell where the focus is going to go after the current onBlur ends to dictate the course of action?
agent002
05-23-2004, 03:27 PM
you mean that you want to focus another element when the onblur of an element is called? Yes:
<input type="text" id="field1" onblur="document.getElementById('field2').focus();">
<input type="text" id="field2">
G Funk
05-23-2004, 03:47 PM
Hmm I think I explained it wrong, I want to know what the next field will be before the onblur of the current field changes so that I can direct control in the current field's code.
I will try explaining with pseudo code:
function calledFromonBlur()
{
if(itemThatWillHaveNextFocus == "whateverField")
{
// do some stuff
}
else
{
// do other stuff
}
}
I'm an idiot, thats the best I can explain what I am looking for.
agent002
05-23-2004, 04:01 PM
Hmm, I don't think you can get the data that way, at least I don't know how that would be done. But you can get it using an onfocus on another element.
Validus
05-24-2004, 02:48 AM
Would the onFocus even happen in this case?
Say you are in a text field, which has onBlur an alert and a reset of the focus into the text field.
If I were to click on a radio button for instance, would that radio button's onFocus even happen?
On the same note, is there a way I can cancel the effects of the onBlur of the text field when a specific radio button is clicked?
agent002
05-24-2004, 05:53 AM
You can test it:
<script type="text/javascript">
document.onfocus = function(e){
var obj = (e && e.target)? e.target : event.srcElement;
alert('Focus! '+obj.tagName);
}
document.onblur = function(e){
var obj = (e && e.target)? e.target : event.srcElement;
alert('Blur! '+obj.tagName);
}
</script>
If you have an input field that is focused, for instance, and click somewhere in the body to blur it, you first get the blur message from the input and then the focus message from the body. If you'd blur it by focusing another input field, you'd get the focus message from that input instead of the body.
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.