6/08/2011

How to add and remove OPTION Elements for HTML SELECTs

This might be something you´ve read 1000 times in other blogs, but I am doing it for the third time in two weeks, so I am going to write it down. This is how you remove all OPTION elements from an HTML SELECT Element (Liferay version):

var element= document.getElementById("<portlet:namespace />theId");
element.length=0;

And this is how you add a selected OPTION Element:


var option= document.createElement("OPTION");
option.text = 'Your text';
option.value = 'Your value';
option.selected = true; 
element.options.add(option); 


If you like this tutorial it would be very nice, if you could click on some of the google ads you see on the right side. It helps me run this block and motivates me ;)

If you have any questions, feel free to leave a comment.