0

I'm attempting to retrieve the ID of the element I'm clicking on

$mainBox.on('click', '.menuBtn', function(event) {
    console.log(event.target.attr('id'))    
});

the element I'm clicking is loaded as part of an external html, this would be the line that is being clicked:

<li class="menuBtn" id="item03"><span class="styleClass1">03</span><span class="styleClass2">Menu Item</span></li>

how do I get item03? event.target.attr('id') doesn't do it

user3024007
  • 283
  • 4
  • 12

1 Answers1

2

Try event.target

event.target is DOM element so you need to make it a jQuery Object to use .attr()


$mainBox.on('click', '.menuBtn', function(event) {
    console.log($(event.target).attr('id'))    
});


or directly use JavaScript
$mainBox.on('click', '.menuBtn', function(event) {
    console.log(event.target.id);
});

$mainBox.on('click', '.menuBtn', function(event) {
    console.log(this.id);
});

this

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107