user1512559
user1512559

Reputation:

Check Listbox items exist using jQuery

I have 2 listboxes (Items moves between them using jquery).Suppose item1 is already selected. Then if I select item1 and item2 and item3 together, only item2 and 3 should inserted in second listbox.

I m not removing item from Listbox1. I need only a checking whether one of the selected items exists in the Listbox2.

//Code
$('#btnAdd').click(function () {

    var selectedOptions = $('#<%=lstAllRole.ClientID %> option:selected');
    if (selectedOptions.length == 0) {
        alert("Please select option to move");
        return false;
    }

    if (selectedOptions.length == 1) {
        if ($("#<%=lstSelectedRole.ClientID %> option[value='" + selectedOptions.val() + "']").length > 0) {
        }
        else {
            $('#<%=lstSelectedRole.ClientID %>').append($(selectedOptions).clone());
        }
    }
    else if (selectedOptions.length > 1) {     // Selecting more than one item to move--only append items which are not in 2nd listbox

       // **I need to validate here**

    }
});

Upvotes: 2

Views: 3107

Answers (3)

Saul Berardo
Saul Berardo

Reputation: 2600

I don't think you need to treat differently the cases when just one or when many options were selected. Just take the list of selected elements and iterate through it, adding to the second list if it is not already there. Something like it should work:

$('#btnAdd').click(function () {

var selectedOptions = $('#<%=lstAllRole.ClientID %> option:selected');

var optionsAlreadyInTheList = $('#<%=lstSelectedRole.ClientID %> option');

if (selectedOptions.length == 0) {
    alert("Please select option to move");
    return false;
} else {
    selectedOptions.each(function() {
        isInTheList = false;
        for(i=0; i< optionsAlreadyInTheList.length; i++) {
            if ($(this).val() == optionsAlreadyInTheList[i].val()) {
                isInTheList = true;
            }
        }
        if( ! isInTheList ) {
            $('#<%=lstSelectedRole.ClientID %>').append($(selectedOptions).clone());
        }
    });        

}
});

This code is just to give an idea. I wrote it without testing and probably won't work as it is.

Upvotes: 0

bipen
bipen

Reputation: 36531

assuming this is what you want..

try this

else if (selectedOptions.length > 1) {     // Selecting more than one item to move--only append items which are not in 2nd listbox

    var tempArray= $.map(selectOptions, function(n){
          return this.value;
    });

    if($.inArray("valueTochekcInSelectedOption", tempArray) != -1) 
    {
        //value not selected in list 1
    }else{
       //value selected in list 1
    }
}

Upvotes: 4

A Coder
A Coder

Reputation: 3046

Just run a foreach function and you will find the items.

//Code

else if (selectedOptions.length > 1) {

    $(selectedOptions).each(function () {
        if ($("#<%=lstSelectedRole.ClientID %> option[value='" + this.value + "']").length > 0) {

            // Do your stuff
        } else {
            $('#<%=lstSelectedRole.ClientID %>').append($(this).clone());
        }
    });
}

Upvotes: 3

Related Questions