Home > Software engineering >  Trying to add a click event to an image with JQuery
Trying to add a click event to an image with JQuery

Time:02-16

I have an image without an ID. The only unique identifiers are src and alt. Not sure what I'm doing wrong with the below but I've been chasing my tail for a while now.

<img  src="/_web/assets/applications/c21140ec-719d-4fe8-aa34-381144127302/c503ca4f-1a36-49df-ae90-75a9226e7e69?X-IoT-Region=westus" alt="fire">

<script>
$('img[alt="fire"]').click(function() {
  alert('clicked');
}); 
</script>

I'm trying to do this in a Chrome browser extension. I've tried it within window.onload and now document.ready. Still clicking does nothing.

CodePudding user response:

It turns out the onl oad event is triggered before the page's app logic starts. So the page actually waits for my extension to complete before continuing so the image doesn't exist yet.

CodePudding user response:

That code works.

Include it between:

    <script>
$(document).ready(function(){
    $('img[alt="fire"]').click(function() {
        alert('clicked');
    }); 

});
    </script>
  • Related