0

Please let me know how can I remove/clear all options from msDropDown. I have tried the below code and its not working fine.

    oHandler2 = $("#main").msDropDown().data("dd"); 
    oHandler2.remove();

Thanks in Advance. Lampy

lambypie
  • 471
  • 1
  • 12
  • 35
  • it looks like remove takes a parameter - the index of the item to remove. – Donal Jun 04 '14 at 15:16
  • Is there any functions available to clear all options? OR Can i get the count of items in the dropdown? – lambypie Jun 04 '14 at 15:19
  • Not sure, you could try the .destroy() method. – Donal Jun 04 '14 at 15:24
  • `.destroy()` turns it into a regular select input. You could then check remove the options using `$('#main options').remove()`, but then he would have to convert back to msDropDown afterwards. – dmullings Jun 04 '14 at 17:57

3 Answers3

2

You need to specify an index when calling the .remove() method and you can get the count of all options by accessing the childElementCount property. Then you just need to remove all the options. Example below:

var oHandler2 = $("#main").msDropDown().data("dd");

for(var i = 0; i < oHandler2.childElementCount; i++){
    oHandler2.remove(0); //remove the current first option
}
dmullings
  • 7,070
  • 5
  • 28
  • 28
1

In my opinion the best way to delete all item is

var oHandler = $("#main").msDropDown().data("dd");
oHandler.set("length", 0);
Rod Kimble
  • 1,302
  • 3
  • 18
  • 44
Kumar_14
  • 574
  • 3
  • 15
0

If you dont need to remove specify item, you can delete your element. And then create new one.

HTML

<div class="mainSection">
    <div id="main"></div>
</div>

Script

<script>
   $("#main").remove();
   $(".mainSection").append("<div id='main'></div>");
   $("#main").msDropDown().data("dd"); 
</script>
Tolga
  • 147
  • 3
  • 4