开发者

Getting sender in jQuery

开发者 https://www.devze.com 2023-02-16 17:27 出处:网络
I have a large list of check boxes all with unique id values. I\'d like to get the id of the checkbox th开发者_开发百科at executes my JavaScript function. Can I do this using $(this)?You can get the t

I have a large list of check boxes all with unique id values. I'd like to get the id of the checkbox th开发者_开发百科at executes my JavaScript function. Can I do this using $(this)?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消