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:
Asked
Active
Viewed 1.2k times
8

broke
- 8,032
- 16
- 54
- 83
-
@Nivas: Your first duplicate link seems right, but definitely not the second one. You should have just voted to close with your first link. – BoltClock May 07 '14 at 15:52
-
@BoltClock Agreed - the second one is not related. I did not intend to post that. Need more coffee. (I already did flag to close and now deleted the second comment) – Nivas May 07 '14 at 15:55
-
I don't really understand what you need, but here my fiddle http://jsfiddle.net/keypaul/kfLf7/1/ – keypaul May 07 '14 at 16:09
2 Answers
8
You can try
var myVal = $('.someclass option:last').val();
$('.someclass').val(myVal);
where someclass is the select class or it's wrapper element class.

alou
- 1,432
- 13
- 16
7
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

gtr1971
- 2,672
- 2
- 18
- 23
-
2This answer was better for me in my case because of duplicate values (fringe case) – Rick Kukiela Jan 17 '18 at 16:35