This question has already been answered:

There is a link presented on the site as a picture: enter image description here one]

I hung a script on it:

$('#header_logo').click(function(){ alert("eqw"); } 

But when you click on an image, the action of the link just happens, i.e. go by href. There is no alert at all. I tried to do the same with headings even: enter image description here

But the result does not change. Shotakoe?


UPD

Thanks Grundy , for the umpteenth time! :) The problem was in the syntax (my eyes ...) and, apparently, in the function:

  $('#page_header').on('click', function(event){ alert("eqw"); }); 

Reported as a duplicate by Grundy members, aleksandr barakin , user194374, Bald , D-side 12 Jul '16 at 10:57 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • The function of adding a handler is called earlier than the necessary element appears in the markup, on the page several elements with the same id and you just click on the wrong one - Grundy
  • after all, it is called only when clicking on a specific element. I click only when the page is fully loaded. - Anque
  • it is called, if it is added, if at the moment of adding this element there is not yet, it will not be added, and, accordingly, it will not be called - Grundy
  • it seems, understood) - Anque
  • and how can I defer the call of the add function? - Anque

1 answer 1

Try this:

 $('#header_logo').click(function(e){ e.preventDefault(); alert("eqw"); }