Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select last item in <select> list using jquery [duplicate]

Tags:

html

jquery

I have a <select> element on my form with the multiple attribute set to "true". After the element gets populated, how can I select the last item in the list using jquery? I want the item to be selected the same way you select it with a mouseclick:

enter image description here

like image 896
broke Avatar asked May 07 '14 15:05

broke


2 Answers

You can try

    var myVal = $('.someclass option:last').val();
    $('.someclass').val(myVal);

where someclass is the select class or it's wrapper element class.

fiddle link

like image 145
alou Avatar answered Nov 15 '22 07:11

alou


You can do this by index value as well:

var num = $('select option').length;
$('select').prop('selectedIndex', num-1); // For choosing last item in list
like image 32
gtr1971 Avatar answered Nov 15 '22 09:11

gtr1971