I've noticed a couple of other posts on here of a similar nature, but none answer my question, which is why I'm posting this.
I'm trying to make a simple form, whereby each input/textarea element turns red when it is triggered by an onblur effect and the value === "". The following code works fine with Crhome and IE, but in Firefox 'event is not defined' - even though I'm passing through an event.
See below code.
//declare gloabl variables
var srcForm = document.myForm;
var nameField = srcForm.nameTxt;
var emailField = srcForm.emailTxt;
var commentsField = srcForm.comments;
//function for field onfocus events
function focusClear (event) {
var eSrc = nuahs.eventUtility.eventSource(event);
if(eSrc.className === "invalid") {
eSrc.className = "";
eSrc.value = "";
}
}
//function for field onblur events
function blurCheck (event) {
var eSrc = nuahs.eventUtility.eventSource(event);
if(eSrc.value === "") {
eSrc.className = "invalid";
eSrc.value = "This field is required";
}
}
//set up event handlers for focus events on each field
nuahs.eventUtility.addEvent(nameField, "focus", function() { focusClear(event)});
nuahs.eventUtility.addEvent(emailField, "focus", function() { focusClear(event)});
nuahs.eventUtility.addEvent(commentsField, "focus", function() { focusClear(event)});
//set up event handlers for focus events on each field
nuahs.eventUtility.addEvent(nameField, "blur", function() { blurCheck(event)});
nuahs.eventUtility.addEvent(emailField, "blur", function() { blurCheck(event)});
nuahs.eventUtility.addEvent(commentsField, "blur", function() { blurCheck(event)});
So, you can see - at the bottom - I'm handling my events with the blurCheck and focusCheck functions, and passing in the event. Therefore I have not a clue why FF does not recognize it!!
The nuahs object is just a simple event utility in another script, but for completeness you can see it below:
var nuahs = new Object();
nuahs.eventUtility = {
addEvent: function() {
if(typeof addEventListener !== "undefined") {
return function(obj, evt, fn) {
obj.addEventListener(evt, fn, false);
};
} else {
return function(obj, evt, fn) {
obj.attachEvent("on" + evt, fn);
};
}
}(),
removeEvent: function() {
if(typeof addEventListener !== "undefined") {
return function(obj, evt, fn) {
obj.removeEventListener(evt, fn, false);
};
} else {
return function(obj, evt, fn) {
obj.detachEvent("on" + evt, fn);
};
}
}(),
eventSource: function(event) {
if (typeof addEventListener !== "undefined") {
return event.target;
} else {
return event.srcElement;
}
},
preventDefault: function() {
if (typeof addEventListener !== "undefined") {
return function(event) {
event.preventDefault();
}
} else {
return function(event) {
event.returnValue = false;
}
}
}(),
stopEvent: function() { //to stop event capture and bubbling
if (typeof addEventListener !== "undefined") {
return function(event) {
event.stopPropagation();
}
} else {
return function(event) {
event.cancelBubble = true;
}
}
}()
}
Any help VERY much appreciated!
See Question&Answers more detail:
os