views:

310

answers:

1

Lets say I have this code

$(document).ready(function()
{
   $('.checkbox').change(function()
   {
      $('.hidden').slideUp('slow', function()
      {
         alert(checkbox value);
      }
   }
}

How do I access the checkboxes value? $(this) doesn't work as you're now in the .hidden element?

+3  A: 

You could capture the value in the outer function:

$(document).ready(function() {
    $('.checkbox').change(function() {
        var $checkbox = $(this);
        $('.hidden').slideUp('slow', function() {
            alert($checkbox.val());
        }
    }
}
Darin Dimitrov
Well, that was easier than I expected... How would I then go about converting $checkbox to a jQuery object so I can get access to .attr() ?
anon
`$($checkbox).attr`
Darin Dimitrov
Worked a treat. Cheers :D
anon
var $checkbox = $(this);
Matt Huggins
@Matt, I've updated my sample to capture the jQuery element instead of the native one.
Darin Dimitrov