C
Chris Lieb
I am trying to add an event listener to the keyup event for some text
inputs that I am creating dynamically. I have no problems getting it
to work in Firefox, but IE just ignores them. I have created a few
functions to aid in making this process work semi-cross-browser. (I
develop in FF, but everyone who uses it will be using IE6.)
function attachEvent(_obj, _evt, _fnc) {
_evt = _evt.toLowerCase();
if (_obj.attachEvent) {
_obj.attachEvent('on' + _evt, _fnc);
} else {
eval('_obj.on' + _evt + ' = ' + _fnc);
}
}
function generateFunction(_name, _params) {
var func;
if (navigator.userAgent.toLowerCase().indexOf('gecko') != -1) {
func = eval('function(){' + _name + '(' + _params + ');}');
} else {
func = function(){eval(_name + '(' + _params + ')');};
}
return func;
}
function attachValidator(_obj, _validatorName) {
if (navigator.userAgent.toLowerCase().indexOf('gecko') != -1) {
attachEvent(_obj, 'keyup', generateFunction(_validatorName, 'this'));
} else {
var eventAttr = document.createAttribute('onkeyup');
eventAttr.value = _validatorName + '(this);';
_obj.setAttributeNode(eventAttr);
}
}
function createTextField(_base, _id, _value, _validatorName, _class) {
var input = document.createElement('input');
var inputID = document.createAttribute('id');
inputID.value = _base + _id;
var inputType = document.createAttribute('type');
inputType.value = 'text';
var inputValue = document.createAttribute('value');
inputValue.value = _value;
input.setAttributeNode(inputID);
input.setAttributeNode(inputType);
input.setAttributeNode(inputValue);
if (_validatorName) {
attachValidator(input, _validatorName);
//attachEvent(input, 'keyup', generateFunction(_validatorName,
'this'));
}
if (_class) {
var inputClass = document.createAttribute('class');
inputClass.value = _class;
input.setAttributeNode(inputClass);
}
return input;
}
Sorry about the browser sniffing using the userAgent string, but since
I am the only non-IE6 user in the building, it shouldn't really matter
in the long run.
Thanks in advance,
Chris
inputs that I am creating dynamically. I have no problems getting it
to work in Firefox, but IE just ignores them. I have created a few
functions to aid in making this process work semi-cross-browser. (I
develop in FF, but everyone who uses it will be using IE6.)
function attachEvent(_obj, _evt, _fnc) {
_evt = _evt.toLowerCase();
if (_obj.attachEvent) {
_obj.attachEvent('on' + _evt, _fnc);
} else {
eval('_obj.on' + _evt + ' = ' + _fnc);
}
}
function generateFunction(_name, _params) {
var func;
if (navigator.userAgent.toLowerCase().indexOf('gecko') != -1) {
func = eval('function(){' + _name + '(' + _params + ');}');
} else {
func = function(){eval(_name + '(' + _params + ')');};
}
return func;
}
function attachValidator(_obj, _validatorName) {
if (navigator.userAgent.toLowerCase().indexOf('gecko') != -1) {
attachEvent(_obj, 'keyup', generateFunction(_validatorName, 'this'));
} else {
var eventAttr = document.createAttribute('onkeyup');
eventAttr.value = _validatorName + '(this);';
_obj.setAttributeNode(eventAttr);
}
}
function createTextField(_base, _id, _value, _validatorName, _class) {
var input = document.createElement('input');
var inputID = document.createAttribute('id');
inputID.value = _base + _id;
var inputType = document.createAttribute('type');
inputType.value = 'text';
var inputValue = document.createAttribute('value');
inputValue.value = _value;
input.setAttributeNode(inputID);
input.setAttributeNode(inputType);
input.setAttributeNode(inputValue);
if (_validatorName) {
attachValidator(input, _validatorName);
//attachEvent(input, 'keyup', generateFunction(_validatorName,
'this'));
}
if (_class) {
var inputClass = document.createAttribute('class');
inputClass.value = _class;
input.setAttributeNode(inputClass);
}
return input;
}
Sorry about the browser sniffing using the userAgent string, but since
I am the only non-IE6 user in the building, it shouldn't really matter
in the long run.
Thanks in advance,
Chris