cooking, programming and everyday life xrado

Thursday, February 19, 2009

Dynamic select boxes

These days i was working on a project, where i needed dynamically loading select boxes. I first tried mootools native methods like removing and injecting new option elements in to select element. That worked just fine in FF, but as you expect in IE did not. IE's select box stood unchanged. I remember while ago i have to re-inject the whole select box to see the changes in IE.

According to DOM, there is a better way. HTMLSelectElement support add and remove method that work in all major browsers. So i have implemented three basic methods for mootools Element class.
Element.implement({
	removeAllOptions: function() {
		if(this.get('tag')!='select') return this;
		for(var i=this.options.length-1;i>=0;i--) this.remove(i);
		return this;
	},

	addOption: function(text,value) {
		if(this.get('tag')!='select') return this;
		var optn = new Element('option');
		if(text) optn.text = text;
		if(value) optn.value = value;
		this.options.add(optn);
		return this;
	},

	removeOption: function(prop,value){
		if(this.get('tag')!='select') return this;
		for(var i=this.options.length-1;i>=0;i--) {
			if (prop=='selected' && this.options[i].selected) this.remove(i);
			if (prop=='value' && this.options[i].value==value) this.remove(i);
			if (prop=='text' && this.options[i].text==value) this.remove(i);
			if (prop=='index' && i==value) this.remove(i);
		}
		return this;
	}
});
Methods hopefully already explaining their self.

USAGE
// remove all options or empty select box
$('selectbox').removaAllOptions();

// add options, one after another
$('selectbox').addOption('One',1).addOption('Two',2).addOption('Two',3);

// removing options 
$('selectbox').removeOption('text','One');
$('selectbox').removeOption('value',2);
$('selectbox').removeOption('selected');
$('selectbox').removeOption('index',0);
happy selecting ;)
A ne bi moralo v USAGE biti napisano addOption in removeOptioon? V implementaciji je namreč zapisano v ednini, v usage pa je uporabljena množina... addOptions in removeOptions...

Lep pozdrav s tvojega bivšega Asus Eee PC.

damn, imas prav ..niso stestirani snipeti ..hvala za info

pozdrav tebi in tvojmu tamalmu eeeji

09:28, Tuesday, April 12, 2011 serggn, from russia
// remove all options or empty select box
$('selectbox').removaAllOptions();

there is a mistake in code: removaAllOptions();

wadawdwadwa

http://www.uggbootsoutlet.com.co
http://www.ralphlaurenoutletonlinepolo.us.com
http://www.ralph-laurenoutlet.me.uk
http://www.uggsale.us.com
http://www.uggoutlet.net.co
http://www.adidasshoesoutlets.us.com
http://www.birkenstocks.us
http://www.nikeshoes.in.net
http://www.cheapjordans.com.co
http://www.moncleroutlets.us.org
http://www.yeezyboost.us.com
http://www.ugg-slippers.us.com
http://www.cheapraybansunglassesofficial.us.com
http://www.coachoutlets.in.net
http://www.ralphlaurenoutletonlines.us.com
http://www.cheapuggssale.com.co
http://www.nikeoutletonline.us.com
http://www.ralph-laurenoutlet.com.co
http://www.beatsbydrdre.org.uk
http://www.uggs-australia.it
http://www.coach-outletonline.us.org
http://www.hermes-handbags.us
http://www.nikeshoes.us.com
http://www.ralph-laurenuk.co.uk
http://www.adidas-yeezyshoes.us.com
http://www.adidasnmd.us.org
http://www.coachoutletsonlines.us.com
http://www.coachfactoryoutletsonlines.us.com
http://www.goosecanadaoutlet.us.com
http://www.nikehuaracheshoes.in.net
http://www.katespade.in.net
http://www.burberry-outlet-canada.ca
http://www.catboots.us
http://www.burberryoutletsale.com.co
http://www.canadagoose-outlet.name
http://www.pandoracharmsuk.uk
http://www.moncleroutlet.in.net
http://www.hermesbirkin.in.net
http://www.coach-factoryoutletonline.us.org
http://www.cheap-uggsboots.us.com
http://www.redbottomsshoes.us.com
http://www.pandora.us.com
http://www.coachhandbagsoutlet.us.com
http://www.supremeuk.org.uk
http://www.michael-korsoutlet.ca">http://www.michael-korsoutlet.ca
http://www.pandoracharmssaleclearance.me.uk
http://www.ralphlaurenoutlet.us.org
http://www.uggs.net.co
http://www.tomsshoes.us.com
http://www.pandoracharms.us.com
http://www.michaelkorsoutletonlineclearance.in.net">http://www.michaelkorsoutletonlineclearance.in.net
http://www.air-jordans.us.com
http://www.kate-spade.us.com
http://www.katespadeoutletonline.us.com
http://www.coachoutletus.us.com
http://www.cheapjordanshoes.us.com
http://www.jordanretro.name
http://www.uggsbootscanada.ca
http://www.nfljerseyscheapwholesale.us.com
http://www.louboutinshoes.in.net
http://www.coachoutlets.us.org
http://canadagoose.jacketsoutlet.us.com
http://www.tomsoutletstore.in.net
http://www.valentinoshoesoutlets.us.com
http://www.cheapuggs.in.net
http://www.christianlouboutinshoes.us.org
http://www.pandora-charms.us.org
http://www.mlbjerseys-cheap.us.com
http://www.pandora-uk.me.uk
http://www.longchamphandbags.me.uk
http://www.mbtshoes.us.org
http://www.red-bottoms.us.com
http://www.michaelkors-handbags.us.org">http://www.michaelkors-handbags.us.org
http://www.moncler-outlet.us.com
http://www.michaelkorsoutletclearance.in.net">http://www.michaelkorsoutletclearance.in.net
http://www.officialcoachoutlet.us.com
http://www.cheapnfl-jerseyswholesale.us.com
http://www.canadagooseoutlet.cc
http://www.coachoutletstoreonlineclearances.in.net
http://www.coachoutletstoreonline.com.co
http://www.nikeairmax.in.net
http://www.yeezyboost-350.co.uk
http://www.louisvuittonoutlet.us.org
http://www.katespadehandbag.us.com
http://www.coachoutlet.in.net
http://www.oakleysunglassses-cheap.us.com
http://www.uggs-outlet.net.co
http://www.michaelkorsoutlet.co.uk">http://www.michaelkorsoutlet.co.uk
http://www.canadagoose-outlet.net.co
http://www.adidasnmd-shoes.us
http://www.ugg-boots.net.co
http://www.raybansunglassessales.us.com
http://www.doudounemonclerpascher.fr
http://www.coachcanadaoutlet.ca
http://www.coachoutletonlinecoachfactoryoutlet.us.com
http://www.ralphlaurensaleclearance.org.uk
http://www.cheap-uggboots.net.co
http://www.uggsaustralia.fr
http://www.cheapraybans.com.co
http://www.ugg-outlet.com.co
http://www.coachoutletcoachfactoryoutletonline.us.com
http://www.canadagooseoutlet.net.co
http://www.christianlouboutinsale.us.org
http://www.oakley.nom.co
http://www.michaelkorshandbags-outlet.us.org">http://www.michaelkorshandbags-outlet.us.org
http://www.coachfactory-outletstore.us.com
http://www.uggsoutlets.com.co
http://www.yeezyboost-350.us.com
http://www.uggsandals.in.net
http://www.birkenstockshoes.us.com
http://www.pandoracharms.name
http://www.guccioutlets.us.org
http://www.birkenstockoutletstore.us.com
http://www.michael-kors-outletonline.us.com">http://www.michael-kors-outletonline.us.com
http://www.cheapjordan-shoes.in.net
http://www.uggsoutlets.in.net
http://www.harden-vol1.com
http://www.jameshardenshoes.us.com
http://www.uggs-outlets.in.net
http://www.pandora-jewelryoutlet.us.com
http://www.ralphlaurenuk.me.uk
http://www.cheapuggboots.com.co
http://www.ultraboost.us
http://www.coach-outlets.us.com
http://www.michaelkors-mkoutlet.in.net">http://www.michaelkors-mkoutlet.in.net
http://www.michaelkorsoutlets.us.com">http://www.michaelkorsoutlets.us.com
http://www.monclerjacketsoutlet.in.net
http://ugg.bootsoutlet.us.com
http://www.adidasyeezyshoes.us.com
http://www.timberland-outlets.us
http://www.pandora-outlet.us.com
http://www.nike-store.us.com
http://www.coach-outlet.in.net
http://www.adidas-nmd.us.com
http://www.adidasnmd.org.uk
http://www.ugg.in.net
http://www.toryburchoutletonline.com.co
http://www.adidas-outlet.us.com
http://www.michaelkorsoutletstoreonline.us.org">http://www.michaelkorsoutletstoreonline.us.org
http://www.cheapmlbjerseys.us
http://www.polo-ralphlauren.in.net
http://www.burberrysale.org.uk
http://www.northfaceoutlet.us.com
http://www.superdryuk.org.uk
http://www.coach-factoryoutlet-online.us.com
http://www.michae

What do you think?
name from url
   can't read the code? (reload)