I have a drop-down list with numerical values:
<select name="a">
<option value="1">asdsadas</option>
<option value="1">wqecsdc</option>
<option value="10">nmnmbn</option>
<option value="16">assadsa</option>
<option value="12">uuyuyuy</option>
<option value="60">xzXz</option>
<option value="55">vbbnbnm</option>
<option value="13">eerrt</option>
</select>
I need to find the highest numerical value within this list. (In this case, it's 60.)
I was thinking of looping using .each
, but is there a shorter way?
This works fine for me, pretty straight forward:
$('select option:last').val()
edit: misunderstood the question but might become handy for others, in most cases you have the options in the select element ordered desc or asc.
Something like this should work:
function findMaxValue(element) {
var maxValue = undefined;
$('option', element).each(function() {
var val = $(this).attr('value');
val = parseInt(val, 10);
if (maxValue === undefined || maxValue < val) {
maxValue = val;
}
});
return maxValue;
}
alert(findMaxValue($('select[name=a]')));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With