42

How can I, using jQuery, set the "next" item of an already selected item as "selected."

For example, if I have:

<select> <option value="1" >Number 1</option> <option value="2" selected="selected">Number 2</option> <option value="3" >Number 3</option> <option value="4" >Number 4</option> </select> 

We can see that "Number 2" is selected, and using jQuery, I'd like to set "Number 3" as selected, and remove the selected "attribute" from "Number 2". I'm assuming I need to use the next selector, but I'm not quite sure how to implement.

1

11 Answers 11

115

Update:

As of jQuery 1.6+ you should use prop() instead of attr() in this case.

The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes.

var theValue = "whatever"; $("#selectID").val( theValue ).prop('selected',true); 


Original Answer:

If you want to select by the value of the option, REGARDLESS of its position (this example assumes you have an ID for your select):

var theValue = "whatever"; $("#selectID").val( theValue ).attr('selected',true); 

You do not need to "unselect". That happens automatically when you select another.

Sign up to request clarification or add additional context in comments.

7 Comments

Your answer is a more rounded answer. Which almost a year later has helped. Thanks.
A year later yes, but I just ran into this code. You made one small mistake with your assumption that the item is automatically unselected when you select another. That is not the case when the option list is multi select. I just wanted to point that out in case someone expected that behavior and was frustrated with it not working as advertised.
Wouldn't .attr('selected',true) add selected attribute to select element instead of option.
@understack. Yes. Yes, it would. We struggled with this very issue this morning. Good catch. +1 We ended up using: $("selectID").find("option[value='"+theValue+"']").attr("selected", "selected") and this worked fine
@jaydel - ok, figured it out by looking at some other forums. It seems that for iPhone4 Safari I must add after the find(blah blah).attr(yada yada) this: $('#sig-mode').change();
|
60
$('option:selected', 'select').removeAttr('selected').next('option').attr('selected', 'selected'); 

Check out working code here http://jsbin.com/ipewe/edit

1 Comment

Is removing the attribute from the currently selected one required? I could be wrong but I think not? The .attr call should be replaced by .prop for any recent version of jquery as far as I know, right?
17

From version 1.6.1 on, it's advisable to use the method prop for boolean attributes/properties such as selected, readonly, enabled,...

var theValue = "whatever"; $("#selectID").val( theValue ).prop('selected',true); 

For more info, please refer to to http://blog.jquery.com/2011/05/12/jquery-1-6-1-released/

Comments

6

you can use

$('option:selected').next('option') 

or

$('option:selected + option') 

And set the value:

var nextVal = $('option:selected + option').val(); $('select').val(nextVal); 

Comments

3

And if you want to specify select's ID:

$("#nextPageLink").click(function(){ $('#myselect option:selected').next('option').attr('selected', 'selected'); $("#myselect").change(); }); 

If you click on item with id "nextPageLink", next option will be selected and onChange() event will be called. It may look like this:

$("#myselect").change(function(){ $('#myDivId').load(window.location.pathname,{myvalue:$("select#myselect").val()}); }); 

OnChange() event uses Ajax to load something into specified div.

window.location.pathname = actual address

OnChange() event is defined because it allowes you to change value not only using netx/prev button, but directly using standard selection. If value is changed, page does somethig automatically.

2 Comments

+1 for stating that ".change()" needs to be called manually. Not knowing it can cause nightmares.
The change was my problem, the value was selected but not showing in the dropdown.
1

This is what i just used, i like how clean it is :-)

$('select').val(function(){ var nextOption = $(this).children(':selected').next(); return $(nextOption).val(); }).change(); 

Comments

1
$('your_select option:selected').next('option').prop('selected', true) 

Comments

0
$('#next').click( function(){ $('#colored_background option:selected').next('option').attr('selected', 'selected'); changeBackgroundColor(); }); 

Working at What is my favorite color?. Click on the arrows.

1 Comment

Cool site! Why not map the image and make the different pieces selectable that way?
0

Find the row, then

var row = $('#yourTable'); 

the value you want to select

var theValue = "5"; row.find("select:eq(2)").find("option[value="+theValue+']').attr('selected','selected'); 

Comments

0
 $("select").prop("selectedIndex",$("select").prop("selectedIndex")+1) 

Comments

0
$('#my_sel').val($('#my_sel option:selected').next().val()); $('#my_sel').val($('#my_sel option:selected').prev().val()); 

Comments