Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get clicked element using jQuery on event?

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); }); 
like image 446
THE JOATMON Avatar asked Apr 18 '13 19:04

THE JOATMON


People also ask

How can get click event value in jQuery?

Try this. $(document). ready(function() { $(this). click(function(event) { alert(event.

How do I get a click 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.

How do you find the value of clicked elements?

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.

How do you call a button click event in jQuery?

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.


1 Answers

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); }); 
like image 60
bipen Avatar answered Sep 30 '22 17:09

bipen