Maladec VampaYa
Maladec VampaYa

Reputation: 351

JQuery. Select multiple get what is got deselected

I know how to get list of selected elements in JQuery:

    var foo = [];
    $("#ddis").change(function() {
        $('#ddis :selected').each(function(i, selected){
            foo[i] = $(selected).text();
            console.log(i+"-"+$(selected).text());
        });
    });

for:

<select id="ddis" name="numbers" multiple="multiple" size="5">
                            <option value="01908298600" selected="selected">01908298600</option><option value="01908298601" selected="selected">01908298601</option><option value="02070079003">02070079003</option><option value="02070079008" selected="selected">02070079008</option><option value="02077239151">02077239151</option><option value="02077239157">02077239157</option>
                        </select>

But actually I need to know what has got selected or deselected every time someone clicks on the element. Like: "the number 01908298600 got deselected", but now I just iterate all selection instead of getting what is different.

Upvotes: 0

Views: 19

Answers (2)

RiccardoC
RiccardoC

Reputation: 866

    var foo = [];
    $("#ddis").change(function() {
        var newFoo = [];
        $('#ddis :selected').each(function(i, selected){
            newFoo[i] = $(selected).text();
            if( foo.indexOf( $(selected).text() ) == -1 ) {
                alert( "Selected " +$(selected).text() );
            }
        });
        foo = newFoo;
    });

JSFIDDLE

Upvotes: 1

void
void

Reputation: 36703

You can use some logic of this kind by storing the previous value and the current value of select.

var current = false;
$("#ddis").change(function() {

       if(current)
            console.log(current+" got deselected");

       var _val =  $(this).val();
       console.log(_val+" got selected");
       current = _val;

});

Upvotes: 1

Related Questions