I am writing some code with jQuery and trying to add an onclick
event listener. When I run my code, the event listener runs once, but does not repeat itself. Why is this happening?
If I replace this with addEventListener('click'...)
it works fine.
$(function(){
var url = "https://www.bensound.com/bensound-img/jazzcomedy.jpg";
var doSomething = {
clickedNow: function(){
alert("Clicked ok now");
}
};
$('#click-me').attr('src',url);
$('#click-me').onclick = doSomething.clickedNow();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<figure>
<img id="click-me" alt="An image went missing here">
<figcaption><figcaption>
</figure>