Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select next option with jQuery

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 !

like image 913
Clément Andraud Avatar asked Jul 19 '12 08:07

Clément Andraud


People also ask

How to select next element in jQuery?

jQuery DOM Traversing Get next element To get the next element you can use the . next() method. If you are standing on the "Anna" element and you want to get the next element, "Paul", the . next() method will allow you to do that.

How to use next in jQuery?

jQuery next() Method The DOM tree: This method traverse forward along the next sibling of DOM elements. Related methods: nextAll() - returns all next sibling elements of the selected element. nextUntil() - returns all next sibling elements between two given arguments.

How to get nextsibling in jQuery?

next( [selector ] )Returns: jQuery. Description: Get the immediately following sibling of each element in the set of matched elements. If a selector is provided, it retrieves the next sibling only if it matches that selector.


5 Answers

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

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

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

like image 132
bugwheels94 Avatar answered Sep 21 '22 11:09

bugwheels94


$("#fieldNext").click(function() {
    $("#selectionChamp > option:selected")
        .prop("selected", false)
        .next()
        .prop("selected", true);
});​

DEMO: http://jsfiddle.net/w9kcd/1/

like image 42
VisioN Avatar answered Sep 21 '22 11:09

VisioN


Pretty simple without jQuery too. This one will loop around to the first option once the last is reached:

function nextOpt() {
  var sel = document.getElementById('selectionChamp');
  var i = sel.selectedIndex;
  sel.options[++i%sel.options.length].selected = true;
}

window.onload = function() {
  document.getElementById('fieldNext').onclick = nextOpt;
}

Some test markup:

<button id="fieldNext">Select next</button>
<select id="selectionChamp">
 <option>0
 <option>1
 <option>2
</select>
like image 22
RobG Avatar answered Sep 23 '22 11:09

RobG


$(function(){
  $('#button').on('click', function(){
    var selected_element = $('#selectionChamp option:selected');
    selected_element.removeAttr('selected');
    selected_element.next().attr('selected', 'selected');

    $('#selectionChamp').val(selected_element.next().val());

  });
});

http://jsbin.com/ejunoz/2/edit

like image 30
Riz Avatar answered Sep 23 '22 11:09

Riz


I would expect such button to loop thru options, and also to trigger change event. Here is possible solution for that:

$("#fieldNext").click(function() {
  if ($('#selectionChamp option:selected').next().length > 0) 
    $('#selectionChamp option:selected').next().attr('selected', 'selected').trigger('change');
  else $('#selectionChamp option').first().attr('selected', 'selected').trigger('change');
});

Here is jsFiddle: http://jsfiddle.net/acosonic/2cg9t17j/3/

like image 24
Aleksandar Pavić Avatar answered Sep 20 '22 11:09

Aleksandar Pavić