dynamic dropdown creation using jquery mobile

4.1k views Asked by At

The following code not working when i create the dropdownlist using jquery mobile

   $(function(){
      $('#hello').append('<select id="myselect" data-native-menu="false">'+
     ' <option>1</option>'+
     '<option>2</option>'+
     '<option>3</option>'+
     '<option>4</option>'+
     '<option>5</option>'+
     '<option>6</option>'+
     '</select>'+
     '<div id="keyOpen" style="background-color: blue;"></div>'+
     '<div id="keyClose"></div>');

     $( "#myselect" ).selectmenu( "refresh" );
     $(document).keyup(function(e){
     if(e.which >= 48 && e.which <= 57){
     $("#myselect").selectmenu( "open" );//this should open the select
     $("#keyOpen").empty().text(e.which);
     }else if(e.which >= 58 && e.which <= 90){
     $("#myselect").selectmenu( "close" );//this should close the select
     $("#keyClose").empty().text(e.which);
     }
     });
    }); 

The same code works fine when i put that code inside the body

Working code:

   <select id="myselect" data-native-menu="false">
     <option>1</option>
     <option>2</option>
     <option>3</option>
     <option>4</option>
     <option>5</option>
     <option>6</option>
  </select>
  <div id="keyOpen" style="background-color: blue;"></div>
  <div id="keyClose"></div>
1

There are 1 answers

0
Omar On

Use $(".selector").selectmenu("refresh") only when doing modifications to an existing select.

Whenever you append a new select, use $(".selector").selectmenu(); to enhance it.

Demo