Select the last option of a drop down

Say you have a list of drop down select options and you want to pick the last option by default when the page loads, with jquery this is how to do it.

$(document).ready(function() {
	$('#my_select option:last-child').attr('selected', 'selected');
});

Below is how you get the the value of the first, last, and the nth options.

var option_first = $('#my_select option:first-child').val();
var option_last = $('#my_select option:last-child').val();
var option_nth = $('#my_select option:nth-child(0)').val();

3 thoughts on “Select the last option of a drop down

  1. LaTonya says:

    I’ll dance at your wedding or if your married I’ll sing at your anniversary. Your *.js solution resolved my forever day issue. Big Thank you !!!! LaTonya, Java Developer

    Like

Leave a comment