Skip to content

Instantly share code, notes, and snippets.

@juandopazo
Created March 16, 2011 22:21
Show Gist options
  • Save juandopazo/873450 to your computer and use it in GitHub Desktop.
Save juandopazo/873450 to your computer and use it in GitHub Desktop.
function createElement(tag) {
return document.createElement(tag);
}
var addOption = function (combo, text, value) {
/* Note by jdopazo:
Lazy initialization for the function _add()
I create a <select> element that I never attach to the dom
and try to attach an <'option'> element to it with try...catch
This way I avoid using try...catch every time this function is called */
var testSelect = createElement('select'),
testOption = createElement('option'),
standards = false;
try {
testSelect.add(testOption, null); //standards compliant
standards = true;
} catch (ex) {
testSelect.add(testOption); // IE only
}
if (standards) {
addOption = function (combo, text, value) {
var newOption = createElement('option');
newOption.text = text;
if (value) {
newOption.value = value;
}
combo.add(newOption, null);
};
} else {
addOption = function (combo, text, value) {
var newOption = createElement('option');
newOption.text = text;
if (value) {
newOption.value = value;
}
combo.add(newOption);
};
}
addOption(combo, text, value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment