I want to load a different image (A bigger version) of existing one when mouse is hovered over that image. So I place all the images through AJAX call
<main class="container">
<ul class="gallery">
</ul>
</main>
success: function (data) {
$.each(data, function (index, element) {
$(".gallery").append($(`<img src="${`images/square/${element.path}`}"
alt="${element.title}"id="${element.path}"city="${element.city}"
taken="${element.taken}"/>`));
});
},
Now I want to load a bigger image (from local disk) of existing image when mouse is hovered and show that image over it (kind of zoom in, but not zoom in over the image but load a bigger image, and append the div to body) in the body.
I’ve tried many different versions of this
(".gallery").mouseenter(function(){
$(this).css("gray");
$(this).append('<div id="preview"');
// console.log($(this).attributes);
// $(this).append($(`<img src="${`images/medium/"${$(this).attr('id')}"`}"
// alt="${$(this).attr('title')}"/>`
// ));
$(this).append('</div>')
})
I am unable to access the attributes dynamically. this.
attribute remains return undefined. Something seems off here. Can you please tell, how to go about it ?
2
Answers
Use arrow function instead of normal fucntion.
Arrow functions do not bind their own
this
, instead, they inherit the one from the parent scope, which is called "lexical scoping".You are getting attribute of
.gallery
which doesn’t have anyid
oralt
yourimg
tag has that so use.find()
to get that particular image where user hover and then get attributes using.attr('yourattributename')
.Demo Code :