-2

I have function which should clear multiselection element values

function multiselectDeselectAll($el) {
   $('option', $el).each(function (element) {
        $el.multiselect('deselect', $(this).val());
   });
}

rendered html is

<select id="myMultiselect" class="multiselect form-control" name="MyNumbers" multiple="multiple" style="display: none;">
   <option value="One">One</option>
   <option value="Two">Two</option>
   <option value="Three">Three</option>
</select>

and I'm calling to deselect like

multiselectDeselectAll('#myMultiselect');

but I'm getting error

TypeError: $el.multiselect is not a function

$el.multiselect('deselect', $(this).val());

ozil
  • 6,930
  • 9
  • 33
  • 56
user1765862
  • 13,635
  • 28
  • 115
  • 220
  • 4
    Because multiselect is not a function. There is no function like multiselect in jquery unless u r using some third party extension or u extended. – Harry Bomrah Nov 17 '15 at 11:02

2 Answers2

0

Use the removeAttr to deselect options

Try:

$('#myMultiselect option').removeAttr('selected');

or

$('option[value="One"],option[value="Two"]').removeAttr('selected');

for a particular set of options

Juicy Scripter
  • 25,778
  • 6
  • 72
  • 93
madalinivascu
  • 32,064
  • 4
  • 39
  • 55
0

As this function doesn't exists, you can do a loop to deselect all options.

var q=document.getElementById('myMultiselect').children; // get elements into "#myMultiselect"
for(var i=0,len=q.length;i<len;i++){ // a loop for each "#myMultiselect" element
    q[i].removeAttribute("selected"); // remove the attribute called selected
}

Note: I used JavaScript functions, but you can use jQuery to make it smaller.