2

Using the .change() event I want to store the value of an attribute of the currently selected option. Please help me finish the below code.

$("#someid").change(function() {
    var myNumber;
});
2
  • @patrick, any one, lets make one up. "myAttr". Commented Feb 14, 2011 at 23:15
  • The reason I had asked was that some attributes are easily accessible as a property. The ID for example. Commented Feb 14, 2011 at 23:20

1 Answer 1

4

If you want need it outside the handler, you'll need to declare the variable outside.

var myNumber;

$("#someid").change(function() {
    myNumber = this.options[ this.selectedIndex ].getAttribute('myAttr');
    // or
    myNumber = $(this).find('option:selected').attr('myAttr');
});

If you only need it inside, then declare the variable inside.

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

1 Comment

@nick, still i can't understand why you need myNumber variable ? :) Patrick's answer will help you.

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.