JavaScript Apply css for a specific hovered image

I'm trying to apply the css for a specific hovered image but my code is applying the css for all images when hover only 1 image.

$(document).ready(function() {
  $('.vidUrl').on('hover', function() {
    $('.thumb-overlay').toggle();
  });
});
.thumb {
  height: 100px;
  width: 100%;
  object-fit: cover;
}

.thumb-overlay {
  background-image: url('http://www.denys.be/assets/images/play.png');
  position: absolute;
  width: 100%;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  background-size: cover;
  background-color: #00000073;
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="position: relative;">
  <a target='_blank' href="#" class="vidUrl">
    <div class="thumb-overlay"></div>
    <img class="thumb" src="http://lorempixel.com/300/200/">
  </a>
</div>
<div style="position: relative;">
  <a target='_blank' href="#" class="vidUrl">
    <div class="thumb-overlay"></div>
    <img class="thumb" src="http://lorempixel.com/300/200/">
  </a>
</div>
Answer:1

$(document).ready(function() {
  $('.VIDEO_PARENT_CLASS').hover(function() {
    $(this).find('.thumb-overlay').toggle();
  });
});
.thumb {
  height: 100px;
  width: 100%;
  object-fit: cover;
}

.thumb-overlay {
  background-image: url('http://www.denys.be/assets/images/play.png');
  position: absolute;
  width: 100%;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  background-size: cover;
  background-color: #00000073;
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="VIDEO_PARENT_CLASS" style="position: relative;">
  <a target='_blank' href="#" class="vidUrl">
    <div class="thumb-overlay"></div>
    <img class="thumb" src="http://lorempixel.com/300/200/">
  </a>
</div>
<div class="VIDEO_PARENT_CLASS" style="position: relative;">
  <a target='_blank' href="#" class="vidUrl">
    <div class="thumb-overlay"></div>
    <img class="thumb" src="http://lorempixel.com/300/200/">
  </a>
</div>
Answer:2

In below code as you wrote. it will apply to all images because of all images have the same class named ".thumb-overlay".

<script type="text/javascript">
    $(document).ready(function(){
        $('.vidUrl').on('hover', function(){
            $('.thumb-overlay').toggle();
        });
    });
</script>

You should change your code as below:

<script type="text/javascript">
        $(document).ready(function(){
            $('.vidUrl').on('hover', function(){
                $(this).find('.thumb-overlay').toggle();
            });
        });
    </script>

As we know $(this) points current object. So it will apply CSS only on current image.

Answer:3

I am trying to iterate an array with forEach and, based on a condition, I do something. For this question, I simplified the condition in order to try to understand what's going on. I expect array b =...

I am trying to iterate an array with forEach and, based on a condition, I do something. For this question, I simplified the condition in order to try to understand what's going on. I expect array b =...

I have a list of images like so: <div> <div class="imgCnr"> <img src="abc.jpg"> </div> <div class="imgCnr"> <img src="def.jpg"> </div&...

I have a list of images like so: <div> <div class="imgCnr"> <img src="abc.jpg"> </div> <div class="imgCnr"> <img src="def.jpg"> </div&...

  1. select parent element css
  2. select parent element javascript
  3. select parent element jquery
  4. select parent element xpath
  5. select parent element sass
  6. select parent element js
  7. select parent element using css
  8. select parent element selenium
  9. select parent element using javascript
  10. select parent element from child css
  11. select parent element
  12. select parent element scss
  13. select parent element css3
  14. select parent element with class
  15. select parent element angular
  16. select parent element using jquery
  17. select parent element html
  18. select parent element from iframe
  19. select parent element of class css
  20. select parent element angularjs

I am trying to call a function that isn't being recognised. I have a PHP block of code that adds the form to the HTML when the user is logged in: <?php if(isset($_SESSION['user'])) { $usr = $...

I am trying to call a function that isn't being recognised. I have a PHP block of code that adds the form to the HTML when the user is logged in: <?php if(isset($_SESSION['user'])) { $usr = $...

  1. onsubmit call javascript function
  2. onsubmit run javascript function
  3. onsubmit call js function
  4. onsubmit javascript call
  5. call onsubmit js
  6. html form onsubmit call javascript function
  7. html onsubmit call javascript function

I have used more effects.The effects likes based on the drag the scroll in browser or going to down of the page in my home page. I have implemented the top button, it can goes top of the page. When i ...

I have used more effects.The effects likes based on the drag the scroll in browser or going to down of the page in my home page. I have implemented the top button, it can goes top of the page. When i ...