Dropdown list add a "pick a class" as default class


#1

I have a dropdown list of classes people are joined too.

New members
Adults
Experienced Adults
and etc.

I’m trying to add a “pick a class” to default class

Pick a class
New members
Adults
Experienced Adults

Code so far

<div class="col-md-4"> 
<script>
$(document).ready(function() {
//////////////////////
$.getJSON("classes-data.php", function(return_data){
	$.each(return_data.data, function(key,value){
	$("#class").append("<option value=" + value.id +">"+value.name+"</option>");		});
});
//////////////////////
////////////                 
$('#class').change(function(){
//var st=$('#class option:selected').text();
var st=$('#class').val();
//alert(st);                    
});
/////////////////////
}); 
</script>
<select id=class id=class>
</select>
</div>

#2

Dont really see a question?


#3
$.getJSON("classes-data.php", function(return_data){ 
  var default = 162;
  $("#class").append("<option selected value=" +default+">"+"pick a class"+"</option>");

  $.each(return_data.data, function(key,value){
     $("#class").append("<option value=" + value.id +">"+value.name+"</option>");
   });
 });



#4

thinsoldier
I tried your code, but now the dropdown is empty.


#5

The dropdown list shows New members as default, would like a default [pick a class]. thinsoldier’s code was what I’m looking for but I couldn’t get it to work.


#6

So add that option as the first thing in the select element with a blank value.


#7

Ok I finally got it. Thank you all for the help.

$.getJSON("classes-data.php", function(return_data){
 $("#class").append('<option selected="selected" value="0">--  Select a Class  -- </option>');	
	
	$.each(return_data.data, function(key,value){
	$("#class").append("<option value=" + value.id +">"+value.name+"</option>");

	});
});

#8

Don’t even do that. Add it to the html itself so that it is always prepoplulated.