0

Continue of question Use classes of dynamically loaded html with Jquery

$(".content-white").on('click', ".product", function () {
    var id = $(".product").attr('id');
    alert(id);
});

How can I get an element with class product to get it's id?

1
  • I highly recommend to read the jQuery tutorial about event handling: "In addition to the event object, the event handling function also has access to the DOM element that the handler was bound to via the keyword this. To turn the DOM element into a jQuery object that we can use jQuery methods on, we simply do $( this ), often following this idiom: var $this = $( this );" Commented Feb 1, 2014 at 16:59

1 Answer 1

2

You can use this to refer to the targeted element within the event handler

$(".content-white").on('click', ".product", function () {
    var id = this.id;//this here refers to the dom element which was targeted by the handler
    alert(id);
});

Inside the Event Handling Function

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.