Search code examples
javascriptjquerydrop-down-menu

Getting recent selection in multiple select box


I want to populate content depending upon the multiple select box selection. I can get all selected value of select box. Using

$('select').change(function(){
   console.info($(this).val()) // it gives me array
});

But i want only the recent selection/deselection user makes.


Solution

  • Try this

    // this will set the last selected or deselected option element's value to the select element as a custom attribute "recent" 
    $("#selectEl option").click(function(event) {
       $(this).parent().attr("recent", $(this).val());
    });
    
    
    $("#selectEl").attr("recent"); // returns the last selected/deselected option value or undefined if attribute recent is not present