Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value of last option of a drop down?

I have dropdown menu..which is dynamic.. How can get value of the last item in that drop down (using jquery is also acceptable)

like image 572
Mohit Jain Avatar asked Mar 18 '10 13:03

Mohit Jain


People also ask

How do you take value from drop down?

To get the value of a select or dropdown in HTML using pure JavaScript, first we get the select tag, in this case by id, and then we get the selected value through the selectedIndex property. The value "en" will be printed on the console (Ctrl + Shift + J to open the console).


1 Answers

With jQuery it's super easy:

var lastValue = $('#idOfSelect option:last-child').val(); 

With plain Javascript it's not much worse:

var theSelect = document.getElementById('idOfSelect'); var lastValue = theSelect.options[theSelect.options.length - 1].value; 
like image 75
Pointy Avatar answered Sep 21 '22 12:09

Pointy