0

Currently my code looks like this,

$("#select").change(function() {
    var thes = $(this).attr('value');
    var next = $('option[:selected]').next().attr('value');
    alert(next);
});

and my select looks like this

<select id="select">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>

I tried using alert the variable thes and it pops up but when I use the variable next it won't. What is wrong with my code? Please help.

1
  • 1
    .attr('value'); can be replaced with .val() Commented Oct 18, 2013 at 18:44

2 Answers 2

4

Change

'option[:selected]'

To

'option:selected'

So:

$("#select").change(function() {
    var thes = $(this).attr('value');
    var next = $('option:selected').next().attr('value');
    alert(next);
});

Check this fiddle

Please note that this would give the next() for the last index as undefined. You may want to handle that appropriately.

Read more on the :selected selector here

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

Comments

0

You missed this

var next = $('option:selected').next().attr('value');

JSFIddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.