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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s