I was wondering how I'd go about finding with jQuery, the element id that called an onClick method? my code is:
function getInfo(event)
{
console.log(event.target.id + " ... ");
}
<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 can't put anything inside the parentheses of the function, because I want a function passed to it later, I just want to be able to tell which called this function.
I know this has been asked many times, and a lot of the questions have answers, but I have read through a lot of them and none have helped me.
Thanks guys
In regards to the answers below at time of this edit, I cannot use the jQuery method like:
$("div.menu-content a").click(function() {
some content here..
});
because I need it to only be run on certain links being clicked, not all of them. Does that help clarify?
$("div.menu-content a").click(function() {
var $elemId = $(this).attr("id");
console.log($elemId + " .... ");
);
Is a much more "jQuery style" solution to do it.
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