Select next option with jQuery

Clément Andraud picture Clément Andraud · Jul 19, 2012 · Viewed 27.3k times · Source

I'm trying to create a button that can select next option.

So, i have a select (id=selectionChamp) with several options, an input next (id=fieldNext), and i try to do that :

$('#fieldNext').click(function() {
    $('#selectionChamp option:selected', 'select').removeAttr('selected')
          .next('option').attr('selected', 'selected');

    alert($('#selectionChamp option:selected').val());      
});

But I can not select the next option.. Thanks !

Answer

bugwheels94 picture bugwheels94 · Jul 19, 2012
$('#fieldNext').click(function() {
    $('#selectionChamp option:selected').next().attr('selected', 'selected');

    alert($('#selectionChamp').val());      
});

Better answer by @VisioN: https://stackoverflow.com/a/11556661/1533609