(function () {
var util = {
//cross browser Events
addEvent: function (el, type, fn) {
if (typeof addEventListener !== "undefined") {
el.addEventListener(type, fn, false);
} else if (typeof attachEvent !== "undefined") {
el.attachEvent("on" + type, fn);
} else {
el["on" + type] = fn;
}
},
removeEvent: function (el, type, fn) {
if (typeof removeEventListener !== "undefined") {
el.removeEventListener(type, fn, false);
} else if (typeof detachEvent !== "undefined") {
el.detachEvent("on" + type, fn);
} else {
el["on" + type] = null;
}
},
getTarget: function (event) {
if (typeof event.target !== "undefined") {
return event.target;
} else {
return event.srcElement;
}
},
preventDefault: function (event) {
if (typeof event.preventDefault !== "undefined") {
event.preventDefault();
} else {
event.returnValue = false;
}
}
};
var form = document.getElementById("theform"),
btn = form.btn,
txt = form.txt,
txtarea = form.txtarea,
select = form.opt,
color = form.color;
util.addEvent(form, "submit", function (e) {
util.preventDefault(e);
});
util.addEvent(btn, "click", function (e) {
var target = util.getTarget(e);
var index = select.selectedIndex;
var opt = select.options[index];
//or selectedOptions which can give a collection and because we only have one option 0 is the index we would use.
var option = select.selectedOptions[0];
// alert(option.value);
select.remove(4); //remove six
//create a new option
var created = new Option("newOption...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.