I'm using the following code to detect when a dynamically generated button is clicked.
$(document).on("click",".appDetails", function () { alert("test"); });
Normally, if you just did $('.appDetails').click()
you could use $(this)
to get the element that was clicked on. How would I accomplish this with the above code?
For instance:
$(document).on("click",".appDetails", function () { var clickedBtnID = ?????? alert('you clicked on button #' + clickedBtnID); });
Try this. $(document). ready(function() { $(this). click(function(event) { alert(event.
An element receives a click event when a pointing device button (such as a mouse's primary mouse button) is both pressed and released while the pointer is located inside the element.
Another way to get the element we clicked on in the event handler is to get it from the event object which is the first parameter in the event handler function. And the following JavaScript code: const onClick = (event) => { console. log(event.srcElement.id); } window.
To trigger the onclick function in jQuery, click() method is used. For example, on clicking a paragraph on a document, a click event will be triggered by the $(“p”). click() method. The user can attach a function to a click method whenever an event of a click occurs to run the function.
As simple as it can be
Use $(this)
here too
$(document).on("click",".appDetails", function () { var clickedBtnID = $(this).attr('id'); // or var clickedBtnID = this.id alert('you clicked on button #' + clickedBtnID); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With