On hover and click to display the image

A few days ago, I asked same question here. I have to show the respective image when hover or click on the anchor tag and It’s solved my issue.

Now, I am getting one more issue in this script. If I use same code one time then there is no issue but If I use same code twice or more then 2-3 times then I am getting the issues. I mean my code is not working properly.

$('.listWrap ul li:eq(0)').addClass('active'); // add class on page load to the first linnk
$('.hoverimg ul li:eq(0)').show(); // show the first image li on page load
$(".listWrap ul li").hover(
  function() {
    $('.listWrap ul li').removeClass('active'); // remove class all of the link li elements
    $('.hoverimg ul li').hide(); // hide all image li elements
    $(this).addClass('active'); // add the class to the current element
    var idx = $(this).index() - 1;
    $('.hoverimg ul li:eq(' + idx + ')').show(); // show the respective image li element
  }
);
.listWrap li.active a {
  color: #f00;
}

.hoverimg li {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">


<section class="">
  <div class="container-fluid">
    <div class="row">

      <div class="col-xxl-6 col-xl-6 col-lg-6 col-md-6 col-sm-12 col-xs-12">
        <div class="listWrap">
          <ul>
            <li><a href="javascript:void(0);">qwerqwer</a></li>
            <li><a href="javascript:void(0);">tyuityui</a></li>
            <li><a href="javascript:void(0);">opoiuiop</a></li>
            <li><a href="javascript:void(0);">mnbvzxcv</a></li>
            <li><a href="javascript:void(0);">zxcmnbv</a></li>
            <li><a href="javascript:void(0);">mnbvzxcv</a></li>
          </ul>
        </div>
      </div>

      <div class="col-xxl-6 col-xl-6 col-lg-6 col-md-6 col-sm-12 col-xs-12">
        <div class="listWrap position-relative">
          <div class="hoverimg">
            <ul>
              <li><img src="https://dummyimage.com/600x400/fa17fa/0011ff" alt=""></li>
              <li><img src="https://dummyimage.com/600x400/000000/0011ff" alt=""></li>
              <li><img src="https://dummyimage.com/600x400/1ee81e/0011ff" alt=""></li>
              <li><img src="https://dummyimage.com/600x400/1e35e6/0011ff" alt=""></li>
              <li><img src="https://dummyimage.com/600x400/e6d620/0011ff" alt=""></li>
              <li><img src="https://dummyimage.com/600x400/fff320/0011ff" alt=""></li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</section>


<section class="mt-5 pt-5">
  <div class="container-fluid">
    <div class="row">

      <div class="col-xxl-6 col-xl-6 col-lg-6 col-md-6 col-sm-12 col-xs-12">
        <div class="listWrap">
          <ul>
            <li><a href="javascript:void(0);">qwerqwer</a></li>
            <li><a href="javascript:void(0);">tyuityui</a></li>
            <li><a href="javascript:void(0);">opoiuiop</a></li>
            <li><a href="javascript:void(0);">mnbvzxcv</a></li>
            <li><a href="javascript:void(0);">zxcmnbv</a></li>
            <li><a href="javascript:void(0);">mnbvzxcv</a></li>
          </ul>
        </div>
      </div>

      <div class="col-xxl-6 col-xl-6 col-lg-6 col-md-6 col-sm-12 col-xs-12">
        <div class="listWrap position-relative">
          <div class="hoverimg">
            <ul>
              <li><img src="https://dummyimage.com/600x400/fa17fa/0011ff" alt=""></li>
              <li><img src="https://dummyimage.com/600x400/000000/0011ff" alt=""></li>
              <li><img src="https://dummyimage.com/600x400/1ee81e/0011ff" alt=""></li>
              <li><img src="https://dummyimage.com/600x400/1e35e6/0011ff" alt=""></li>
              <li><img src="https://dummyimage.com/600x400/e6d620/0011ff" alt=""></li>
              <li><img src="https://dummyimage.com/600x400/fff320/0011ff" alt=""></li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</section>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

Answer

You have select the specific li elements with the selector –

$('.listWrap ul li:eq(0)').addClass('active'); // add class on page load to the first linnk
$('.hoverimg ul li:eq(0)').show(); // show the first image li on page load
$(".listWrap ul li").hover(
  function() {
    $(this).closest('.listWrap').find('ul li').removeClass('active'); // remove class all of the link li elements
    const $hoverImage = $(this).closest('.row').find('.hoverimg')
    $hoverImage.find('ul li').hide();
    $(this).addClass('active'); // add the class to the current element
    var idx = $(this).index() - 1;
    $hoverImage.find('ul li:eq(' + idx + ')').show(); // show the respective image li element
  }
);
.listWrap li.active a {
  color: #f00;
}

.hoverimg li {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">


<section class="">
  <div class="container-fluid">
    <div class="row">

      <div class="col-xxl-6 col-xl-6 col-lg-6 col-md-6 col-sm-12 col-xs-12">
        <div class="listWrap">
          <ul>
            <li><a href="javascript:void(0);">qwerqwer</a></li>
            <li><a href="javascript:void(0);">tyuityui</a></li>
            <li><a href="javascript:void(0);">opoiuiop</a></li>
            <li><a href="javascript:void(0);">mnbvzxcv</a></li>
            <li><a href="javascript:void(0);">zxcmnbv</a></li>
            <li><a href="javascript:void(0);">mnbvzxcv</a></li>
          </ul>
        </div>
      </div>

      <div class="col-xxl-6 col-xl-6 col-lg-6 col-md-6 col-sm-12 col-xs-12">
        <div class="listWrap position-relative">
          <div class="hoverimg">
            <ul>
              <li><img src="https://dummyimage.com/600x400/fa17fa/0011ff" alt=""></li>
              <li><img src="https://dummyimage.com/600x400/000000/0011ff" alt=""></li>
              <li><img src="https://dummyimage.com/600x400/1ee81e/0011ff" alt=""></li>
              <li><img src="https://dummyimage.com/600x400/1e35e6/0011ff" alt=""></li>
              <li><img src="https://dummyimage.com/600x400/e6d620/0011ff" alt=""></li>
              <li><img src="https://dummyimage.com/600x400/fff320/0011ff" alt=""></li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</section>


<section class="mt-5 pt-5">
  <div class="container-fluid">
    <div class="row">

      <div class="col-xxl-6 col-xl-6 col-lg-6 col-md-6 col-sm-12 col-xs-12">
        <div class="listWrap">
          <ul>
            <li><a href="javascript:void(0);">qwerqwer</a></li>
            <li><a href="javascript:void(0);">tyuityui</a></li>
            <li><a href="javascript:void(0);">opoiuiop</a></li>
            <li><a href="javascript:void(0);">mnbvzxcv</a></li>
            <li><a href="javascript:void(0);">zxcmnbv</a></li>
            <li><a href="javascript:void(0);">mnbvzxcv</a></li>
          </ul>
        </div>
      </div>

      <div class="col-xxl-6 col-xl-6 col-lg-6 col-md-6 col-sm-12 col-xs-12">
        <div class="listWrap position-relative">
          <div class="hoverimg">
            <ul>
              <li><img src="https://dummyimage.com/600x400/fa17fa/0011ff" alt=""></li>
              <li><img src="https://dummyimage.com/600x400/000000/0011ff" alt=""></li>
              <li><img src="https://dummyimage.com/600x400/1ee81e/0011ff" alt=""></li>
              <li><img src="https://dummyimage.com/600x400/1e35e6/0011ff" alt=""></li>
              <li><img src="https://dummyimage.com/600x400/e6d620/0011ff" alt=""></li>
              <li><img src="https://dummyimage.com/600x400/fff320/0011ff" alt=""></li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</section>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>