Wai Yan Hein
Wai Yan Hein

Reputation: 14791

How to retrieve the attribute from event object of jQuery?

I am developing a web application. In my app I am using jQuery. But I am having a problem or a thing that I am so curious with jQuery that is I want to retrieve the attribute of event object not using $(this).

This is formal way

$('.selector').on('click',function(e){
  alert($(this).attr('attribute')) // I am retrieving attribute using $(this)
})

But this is what I want

$('.selector').on('click',function(e){
  alert(e.attr('attribute')) // I am retrieving from e
})

I think you understand my second code. That is how I want to retrieve using event object. Is that possible?

Upvotes: 0

Views: 35

Answers (1)

adeneo
adeneo

Reputation: 318182

e in your code is just the event object (interface).

You're probably looking for the event.target and you'd have to wrap that in jQuery to use attr()

$('.selector').on('click',function(e){
    alert( $(e.target).attr('attribute') );
});

Upvotes: 3

Related Questions