0

In my Java Script, I am using a select tag to select multiple values one after the other,

My requirement is passing this newly selected value to a function.

Everything works perfect with jQuery on Change function, but when I deselect the item from the list still it taking the item and passing it to function.

How can I clear this problem?

 $("#clients").off('change').on('change', function(){
 selectedclientname = $(this).closest('select').find('option').filter(':selected:last').text();
 selectedclientid = $(this).closest('select').find('option').filter(':selected:last').val();
  function1(selectedclientid);
 });

enter image description here

Barmar
  • 741,623
  • 53
  • 500
  • 612
Gopikrishn
  • 35
  • 1
  • 16
  • ` $('#clients').find(":selected").text();` use this, u will get selected value – Durga May 25 '17 at 05:32
  • 1
    What type of element is `#clients`? If it's the ` – Barmar May 25 '17 at 05:36
  • Yes, its a select and working good, My problem is when I unselect an item from the list it's taking that value. So how can I restrict on('change' ) on unselect? – Gopikrishn May 25 '17 at 05:57
  • Is there any way to prevent deselect option in this query, It should work only when we select a new item. But, in my solution when I deselect the item from the list then it is giving last item from the new list., thanks – Gopikrishn Jun 20 '17 at 05:56

1 Answers1

2

Finally I figure it out by the following way

   $("#clients").off('change').on('change', function(evt, params){              
         var itemSelected = false;
         console.log("::::::::::::::deselected"+ params.deselected);
         console.log("::::::::::::::selected"+ params.selected);
         if(params.selected != null)
             {
               itemSelected  = true;
             }
         else{
             itemSelected  = false;
             }
      });
Gopikrishn
  • 35
  • 1
  • 16