Matt
Matt

Reputation: 295

jQueryUI Spinner 'undefined' error with $(this)

I'm trying to change the background colour of a spinner control when it's value is modified.

I have the following code:

<h2>SpinnerTest</h2>
<input id="spinner1" type="spinner" value="5" />
<input id="spinner2" type="spinner" value="10" />

<script type="text/javascript">
    //Line1:  $("input[type='spinner']").spinner({ min: 0, max: 25, stop: function () { alert(this.id); } });
    //Line2:  $("input[type='spinner']").spinner({ min: 0, max: 25, stop: function () { alert($(this).id); } });
</script>

When I use Line1 I get an alert with the correct spinner control ID. However, when I use Line2 I get an alert with 'undefined'.

Ultimately, I want to call toggleClass() on the spinner element. Hence why I'm trying to use $(this).

I'm relatively new to jQuery - could someone help explain why $(this) doesn't work?

Upvotes: 0

Views: 919

Answers (1)

Daedalus
Daedalus

Reputation: 7722

.id is not a valid property for the jquery object. To get the id of an element, use .attr(): $(this).attr("id").

The .attr() method, if only one parameter is supplied, gets the corresponding attribute's value. If a second parameter is supplied, it changes that value to the second parameter supplied.

Upvotes: 1

Related Questions