Get information about the element that called the function using jQuery

I was wondering how I will search with jQuery, an identifier for an element called the onClick method? my code is:

Js

function getInfo(event) { console.log(event.target.id + " ... "); } 

HTML

 <div class="menu-content" id="thisismyid"> This is some placeholder text.<br> <a onClick="getInfo()" id="thisisthelinksid">Click on me</a> </div> <div class="menu-content" id="thisismysecondid"> This is some more placeholder text.<br> <a onClick="getInfo()" id="thisistheotherlinksid">Click on me</a> </div> 

I cannot put anything inside the parentheses of a function, because I want the function to be passed in later, I just want to say who called this function.

I know that this has been asked many times, and many questions have answers, but I read them a lot, and none of them helped me.

Thanks guys,

-Edit -

As for the answers below during this edit, I cannot use the jQuery method, for example:

 $("div.menu-content a").click(function() { some content here.. }); 

because I need it to run only on certain clicks, and not on all. Did this clarification help?

+4
source share
3 answers
 $("div.menu-content a").click(function() { var $elemId = $(this).attr("id"); console.log($elemId + " .... "); ); 

This is a much more β€œjQuery style solution” to do this.

+5
source

Try:

 function getInfo(event) { var id = $(event.target).attr('id'); console.log(id); } 
+2
source

Do not use onclick attributes, use event handlers.

 $(function(){ $('a', 'div.menu-content').click(function(){ console.log(this.id+'...'); }); }): 
+1
source

All Articles