1
 $('#remove').click(function() {
        var foo = [];
        $('#FeatureLists :selected').each(function(i, selected) {
            foo[i] = $(selected).text();
            alert(foo[i]);
            if (foo[i] != "Add" )
                return !$('#FeatureLists option:selected').remove();
            if (foo[i] != "Edit")
                return !$('#FeatureLists option:selected').remove();
        });

    });

i have six items in my select in which 4 of them are add,edit ,delete view, it is multiselect list, i don't want the user to remove the these 4 items , apart from that they can remove any item. how will i do that? it is not happening in the above code

2 Answers 2

2

First of all, based on your sample code, the array foo is does not appear to be needed. Secondly, the if statement needs to include all the items that need to be excluded with OR conditions as shown below:

if (foo[i] == "Add"    || 
    foo[i] == "Edit"   || 
    foo[i] == "Delete" || 
    foo[i] == "View")
{
     return;             
}
else
{
     $(selected).remove();
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try this as well. Simple one line code and slightly faster

$(document).ready(function() {
         $("input").click(function(event) {
            var foo = [];
            $('#FeatureLists :selected').each(function(i, selected) {
               foo[i] = $(selected).text();
               if (foo[i] != "Add" && foo[i] != "Edit" && foo[i] != "Delete" && foo[i] != "View") return $(selected).remove();
            });
         });
      });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.