Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting sender in jQuery

I have a large list of check boxes all with unique id values. I'd like to get the id of the checkbox that executes my JavaScript function. Can I do this using $(this)?

like image 956
esqew Avatar asked Mar 13 '11 05:03

esqew


2 Answers

You can get the target of the event using event.target.

$('input:checkbox[id]').change(function(event) {
  var checkboxID = $(event.target).attr('id');
  alert(checkboxID);
});

JSfiddle Demo

like image 144
Andrew Moore Avatar answered Oct 02 '22 12:10

Andrew Moore


If this points to your checkbox, then you would get the id using $(this).attr('id')

For example:

$('input:checkbox').click(function(){
    var id = $(this).attr('id');
    // do something with id
});

See DEMO.

like image 37
rsp Avatar answered Oct 02 '22 11:10

rsp