The Muffin Man
The Muffin Man

Reputation: 20004

Help with jQuery selector for select box

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;
});

Upvotes: 2

Views: 247

Answers (1)

user113716
user113716

Reputation: 322492

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.

Upvotes: 4

Related Questions