Feb9

Select the last option of a drop down

Posted by: Brian Chan | Filed in: technology | Tags: | 04:32 pm, February 9th, 2012 Add comments

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.

1
2
3
$(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.

1
2
3
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();



Related posts:

  1. Disallow de-select when select-all is checked
  2. Stop event propagation
  3. dataTables editable plugin multiple select
  4. PHP read from a mounted drive
  5. PHP command line memory limit error

Leave a Reply