0

When the page loads i want to sort the dropdown using the jquery, to show the BI option first in the dropdown. I am generating the dropdown from the database.

            <select id="conversionPixels" name="conversionPixels" >
                    <option value="10">PA3G ABTestConversion1DayWindow</option> 
                    <option value="11">activ Conversion</option>            
                    <option value="12">Proactiv Plus 24 Hour View</option>  
                    <option value="13">Proactiv Plus Conversion</option>    
                    <option value="0">BI</option>   
            </select>

I have used below jquery, it is not working

             $("#conversionPixel").each(function() {

                // Keep track of the selected option.
                var selectedValue = $(this).val();

                // Sort all the options by text. I could easily sort these by val.
                $(this).html($("option", $(this)).sort(function(a, b) {
                    if( a.text=='BI'){
                        return -1;
                    }
                     if( b.text=='BI'){
                         return 1;
                    }
                    return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
                }));

                // Select one option.
                $(this).val(selectedValue);
            });
user3157090
  • 517
  • 3
  • 12
  • 29

4 Answers4

0

So why not to do something like

When you get data from database make sure BI is first Use code like

select id,name 
from friends 
order by id=5 desc, id asc

And then just use foreach to create options for your select - BI will be first

konik
  • 173
  • 1
  • 2
  • 8
0

Use this code snippet:

  $("#conversionPixels > option").each(function() {
           if(this.text == "BI"){
                 $(this).remove();
                 $("#conversionPixels").prepend($(this));
           }
   });
Satya Ranjan Sahoo
  • 1,086
  • 7
  • 24
0

If I understood your question correctly, all you want is the <option value="0">BI</option> element to be first on the select. Try runing the code snippet below.

$("#conversionPixels").children().each(function(index, elem) {
  var $elem = $(elem);
  if ($elem.val() === "0") {
    $elem.remove();
    $("#conversionPixels").prepend($elem);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="conversionPixels" name="conversionPixels">
  <option value="10">PA3G ABTestConversion1DayWindow</option>
  <option value="11">activ Conversion</option>
  <option value="12">Proactiv Plus 24 Hour View</option>
  <option value="13">Proactiv Plus Conversion</option>
  <option value="0">BI</option>
</select>
JME
  • 3,592
  • 17
  • 24
0

This works if the select option text value you want to change is always BI.

//copy values BI
var html = $('select option').filter(function () { return $(this).html() == "BI"; }).html(),
value = $('select option').filter(function () { return $(this).html() == "BI"; }).val();

//remove option BI
$('select option').filter(function () { return $(this).html() == "BI"; }).remove();


//add to start copied value BI            
$('select').prepend("<option selected value ='"+value+"'>"+html+"</option>");
user3566111
  • 158
  • 6