Select option removal function

The following function is supposed to remove a selected value from the selectbox, and update a hidden field with all values.

I have two problems with it: Lets say I have the following options in the selectbox : 1071,1000,1052 After removing the first option (1071), the hidden field is getting a value of 1052,1000,1000,

if I remove the second option (1000) the hidden field is 1052,1052,1071 if I remove the third one (1052), I get an options[...].value is null or not an object

Can someone plz help me fix this ?

function removeOptions() {
    var selectbox = document.getElementById('zipcodes');
    var zipcodes = document.getElementById('zip');
    var tmpArray = [];
    var i;
    for (i = selectbox.options.length - 1; i >= 0; i--) {
        if (selectbox.options[i].selected){
            selectbox.remove(i);
        }
        tmpArray.push(selectbox.options[i].value);
    }
    zipcodes.value = tmpArray.join(',');

}

Answers


Assuming you don't need the selected value in hidden value, place the portion to push in tmpArray in else part

    if (selectbox.options[i].selected){
                selectbox.remove(i);
    }else{
            tmpArray.push(selectbox.options[i].value);
    }

Need Your Help

C++ sorting a vector or linked list

c++ algorithm

I have an input file that I want to sort based on timestamp which is a substring of each record. I want to store multiple attributes of the

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.