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>
Use
$(".selector").selectmenu("refresh")
only when doing modifications to an existingselect
.Whenever you append a new
select
, use$(".selector").selectmenu();
to enhance it.