tags:

views:

31

answers:

1

I have the following a drodown list with numerical values

eg

<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 dropdown, in this case it's 60.

I was thinking of looping using .each but is there a shorter way?

+1  A: 

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]')));
Ken Browning