techV
techV

Reputation: 935

How to remove Options from multiselect dropdown in Jquery

I have some table column with some preselected values and now i want to remove those selected values from dropdown(ddlMultiselect) .. Both table column and dropdown option values are same and what i want that those values should be hide/remove from dropdown as per if condition.

$('#sometabletr:gt(0)').each(function () {
            var row = $('td:eq(0) > span', this).text();
            $('#ddlMultiselect :selected').each(function () {
                var col = $(this).val();
                if (row == col) {
                    $(this).remove();
                 }
            });
        });

Upvotes: 7

Views: 3964

Answers (2)

Moe9977
Moe9977

Reputation: 259

This is the way is do it, fast and easy way

            $('#listname option:selected').each(function (index, option) { 
                $(option).remove(); 
            });      

Upvotes: 2

Jeremy J Starcher
Jeremy J Starcher

Reputation: 23863

There is another way to approach this issue.. but setting up classes on the table rows, all you have to do is change the class of the table element itself to hide/show vast amounts of things while only doing a single repaint, which GREATLY improves performance.

In this example, I have the adding of a class hard-coded, but you could use jQuery's addClass and removeClass or look up the best alternatives available.

<doctype html>
<html>
   <header>
      <title>Demo HIde</title>

      <style>
      #mytable.even tr.odd {
        display:none;
      }   
      </style>


   </header>
   <body>


   <table id="mytable">
   <tr class="odd"><td>1</td></tr>
   <tr class="even"><td>2</td></tr>
   <tr class="odd"><td>3</td></tr>
   <tr class="even"><td>4</td></tr>
   <tr class="odd"><td>5</td></tr>
   <tr class="even"><td>6</td></tr>

   </table>

   <script>
   // Show the even values only
   document.getElementById("mytable").className += " even";
   </script>


   </body>
</html>

Upvotes: 1

Related Questions