jQuery to show images in sequence in a div depending on their ID

Viewed 26

I am having total 10 images with ID's in a div. Now I want to show those images depending on arrow keypress in a specific sequence 1a, cv2, w3i, e4, re5, we6i, w7i, w8i, in, xxi irrespective of their position in div. Now, when the arrow press second time then it should show second image in that series. Also, if the image in the div are less than 10 suppose 4. Then also, they should show in the same sequence as they show, when there were 10 imges. Please look at my example,

$("img").hide();
//pressing arrow button first time
$(document).keydown(function(e) {
  if (e.which == 39) {
  //show 1a
  //if 1a does not exist, then show cv2
  //if 1a,cv2 does not exist, then show w3i
  //if 1a,cv2,w3i does not exist, then show e4
  //and so on.
  }
});

//pressing arrow button second time
$(document).keydown(function(e) {
  if (e.which == 39) {
  //show the second img in series after as first already shown in first press
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Example1: If div has 10 images -->
<div class="img">
  <img id="xxi" src="#">
  <img id="re5" src="#">
  <img id="w8i" src="#">
  <img id="w3i" src="#">
  <img id="we6i" src="#">
  <img id="1a" src="#">
  <img id="w7i" src="#">
  <img id="in" src="#">
  <img id="cv2i" src="#">
  <img id="e4" src="#">
</div>

<!-- Example2: If div has 4 images -->
<div class="img">
  <img id="w3" src="#">
  <img id="w7" src="#">
  <img id="1a" src="#">
  <img id="e4" src="#">
</div>

1 Answers

Your updated question is a bit confusing because you added in, xxi in which I don't see any number number-based logic will fail. However, I found your question interesting, So I tried it.

Here is my example, I am getting all the hidden images from the selector then I am mapping the selectors' ids and also extracting the number from ids and saving it as index then I am sorting by index. once sorting is done. I pop the first object mapped array and then I get the ID from the array and I use jquery to show that image ID.

(function($) {
  $('.img > img').hide();

  const showNext = ($el) => {
    const els = $($el + ' > img:not(:visible)');
    if (els.length) {
      const ids = els
        .map(function() {
          const id = this.id;
          return {
            id: id,
            index: id.replace(/[^0-9]/g, ''),
          };
        })
        .get()
        .sort((a, b) => a.index - b.index);

      $($el + ' > img#' + ids[0].id).show();
    }
  };

  //pressing arrow button first time
  $(document).keydown(function(e) {
    if (e.which == 39) {
      showNext('#first-container');
      showNext('#second-container');
    }
  });
})($);
.img {
  display: grid;
  gap: 15px;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  margin-bottom: 60px;
  counter: img;
}

.img img {
  max-width: 100%;
  height: auto;
  counter-increment: img;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous" />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

<body>
  <div class="container my-4">
    <h3>Press right arrow key to see the images</h3>
    <!-- Example1: If div has 10 images -->
    <div class="img" id="first-container">
      <img id="10i" src="https://placehold.jp/150x150.png?text=10i" width="150" height="150" />

      <img id="5i" src="https://placehold.jp/150x150.png?text=5i" width="150" height="150" />

      <img id="8i" src="https://placehold.jp/150x150.png?text=8i" width="150" height="150" />

      <img id="3i" src="https://placehold.jp/150x150.png?text=3i" width="150" height="150" />

      <img id="6i" src="https://placehold.jp/150x150.png?text=6i" width="150" height="150" />

      <img id="1i" src="https://placehold.jp/150x150.png?text=1i" width="150" height="150" />

      <img id="7i" src="https://placehold.jp/150x150.png?text=7i" width="150" height="150" />

      <img id="9i" src="https://placehold.jp/150x150.png?text=9i" width="150" height="150" />

      <img id="2i" src="https://placehold.jp/150x150.png?text=2i" width="150" height="150" />

      <img id="4i" src="https://placehold.jp/150x150.png?text=4i" width="150" height="150" />
    </div>

    <!-- Example2: If div has 4 images -->
    <div class="img" id="second-container">
      <img id="i3" src="https://placehold.jp/150x150.png?text=i3" width="150" height="150" />

      <img id="i7" src="https://placehold.jp/150x150.png?text=i7" width="150" height="150" />

      <img id="i1" src="https://placehold.jp/150x150.png?text=i1" width="150" height="150" />

      <img id="i4" src="https://placehold.jp/150x150.png?text=i4" width="150" height="150" />
    </div>
  </div>
</body>

</html>

Related