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

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>

Dont really see a question?

$.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>");
   });
 });


1 Like

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

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.

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

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>");

	});
});

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

Sponsor our Newsletter | Privacy Policy | Terms of Service