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.
you can use
or
And set the value:
This is what i just used, i like how clean it is :-)
Update:
As of jQuery 1.6+ you should use
prop()
instead ofattr()
in this case.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):
You do not need to "unselect". That happens automatically when you select another.
And if you want to specify select's ID:
If you click on item with id "nextPageLink", next option will be selected and onChange() event will be called. It may look like this:
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.