-1
<div>

  <h1 class="h1 myh1">Headline</h1>

</div>

(function($){
    var var_h1 = $('h1') 
    $('div').on('click', function(event){
    if (event.target == var_h1 ) {
        alert('H1 was clicked')
    }
  })
})(jQuery)

When I click h1 nothing is happening.

How can I use event.target and check if the element clicked is the variable var_h1?

I don't wish to check a class if exist because this variable will be reused again and again. How can I using event.target check for the variable var_h1 if clicked?

Thanks in advance.

babarosa89
  • 37
  • 5

1 Answers1

-1

Your problem is the comparison:

event.target == var_h1

var_h1 is a jQuery object, not a native HTML element reference. So instead you need:

event.target === var_h1[0]

or

event.target === var_h1.get(0)
Mitya
  • 33,629
  • 9
  • 60
  • 107