For some reason when I click on an image with this JQuery code the alert screen doesn't show up.
HTML:
<div id="example1">
<div>
<div>
<div class="user">
<img class="Image" src="images/image.jpg">
</div>
</div>
</div>
</div>
Javascript:
$(document).ready(function(){
$("img").click(function(){
alert("it works!");
});
});
I cant figure out why this isn't working I included the jquery library and the <script>
tag is under the div
on('click') event in jquery . on('click') is different from . click(), there it has the capacity to create delegated event handlers by passing a selector parameter, while . click() does not.
Update Mouse Drivers It's prudent to make sure your mouse drivers are always up-to-date. If the left click isn't working, you definitely need to check them. Right-click on the Start Menu and then choose Device Manager.
So onclick creates an attribute within the binded HTML tag, using a string which is linked to a function. Whereas . click binds the function itself to the property element.
To trigger the onclick function in jQuery, click() method is used. For example, on clicking a paragraph on a document, a click event will be triggered by the $(“p”). click() method. The user can attach a function to a click method whenever an event of a click occurs to run the function.
the img isn't in the DOM when your event handler is registered. you can use $('body').on('click','img',function(){alert('it works');})
Rather than run your code in document.ready(), you should use window.load() function instead.
$(window).load(function() {
$("img").click(function(){
alert("it works!");
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With