$(this).closest not working when added to script


$(this).closest not working when added to script



Hello I have a very basic script that moves the child <img> inside its <label> parent when you hover on and off the <label> element.


<img>


<label>


<label>



The issue is if you hover over one label. ALL images under ALL labels move.. This i do not want. I tried to solve this by adding $(this).closest to my function. But when $(this).closest code is added it breaks. If you remove (this).closest from my code it works fine but its affecting all of them instead of the single one being hovered over.


$(this).closest


$(this).closest



HTML









</div>



jQuery


$(".checkbox-cover.images-true>div>label").hover(
function () {
$(this).closest('img').stop().animate({top: '-200px'});
}, function (){
$(this).closest('img').stop().animate({top: '0'});
});





closest searches the DOM tree going UP, not DOWN, what you really want is $(this).find('> img') instead.
– Matteo Tassinari
Jul 2 at 13:28



closest


$(this).find('> img')





So how do i make the script work by only affecting the current hovered element not all with that class?
– KYSSE
Jul 2 at 13:29





Replace closest with find!
– eisbehr
Jul 2 at 13:29


closest


find





I assumed "this" was refering to the <label> ??
– KYSSE
Jul 2 at 13:29





This is once again a prime example of making stuff harder on yourself than it had to be, by using JS to implement something that can be done in CSS already …
– CBroe
Jul 2 at 13:30





2 Answers
2



closest searches the DOM tree going up (ancestors), not down (descendants), what you really want is find instead.


closest


find


$(".checkbox-cover.images-true > div > label").hover(
function () {
$(this).find('> img').stop().animate({top: '-200px'});
}, function () {
$(this).find('> img').stop().animate({top: '0'})
});
});



Lastly, as the comments suggest, you can shorten $(this).find('> img') with $('img', this), setting the "context" parameter.


$(this).find('> img')


$('img', this)





Or even simpler $('img', $(this))
– marekful
Jul 2 at 13:30


$('img', $(this))





Or even better $('img', this) ;)
– eisbehr
Jul 2 at 13:30



$('img', this)





Thanks for your suggestions, I updated my answer!
– Matteo Tassinari
Jul 2 at 13:36



Since img is a child of label here and closest is used to get the closest parent that matches the selector. Try this instead:


img


label


closest


$(".checkbox-cover.images-true>div>label").hover(
function() {
$('img', this).stop().animate({top: '-200px'});
},
function() {
$('img', this).stop().animate({top: '0'});
}
);



Also, you can achieve this with CSS only like:




.images-true label img {
position: relative;
transition: all 1s ease 0s;
top: 0;
}

.images-true label:hover img {
top: -200px;
}







</div>



Just wanted to let you know about it and keep all your options open.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

PHP contact form sending but not receiving emails

PHP parse/syntax errors; and how to solve them?

iOS Top Alignment constraint based on screen (superview) height