onClick triggers all elements. I want to only trigger the specified element

Viewed 100

I have searched StackOverflow and online forums but haven't gotten an answer for this issue, although I believe this is a basic question, but I'm lost.

Let's presume I have a row with two columns, each column has an audio player.

I have customized the style of the audio player through JavaScript by adding and removing classes. And programmed its functionalities also through JavaScript.

everything is working and functional, but When I add a second audio player, then they both trigger the JavaScript code and start playing. Same goes if I add a third or fourth.

My question is: how do I make the audio players play independently from each other?

I have came across this in other places with JavaScript too, I seem to always have a problem of executing my block of code only for the element that called it and not every element on the page that shares the same attributes.

function playAudio(val) {
  var aud = $("audio")[val - 1];

  $(".play-pause").on("click", function () {
    if (aud.paused) {
      aud.play();
      aud.loop = false;
      $(".play-pause").removeClass("icon-play");
      $(".play-pause").addClass("icon-stop");
    } else {
      aud.pause();
      $(".play-pause").removeClass("icon-stop");
      $(".play-pause").addClass("icon-play");
    }
  });

  $(".next").on("click", function () {
    //Replace the below source with the next audio file source
    aud.src = "another audio source";
  });

  aud.ontimeupdate = function () {
    $(".progress").css("width", (aud.currentTime / aud.duration) * 100 + "%");

    var mins = Math.floor(aud.currentTime / 60);
    if (mins < 10) {
      mins = "0" + String(mins);
    }
    var secs = Math.floor(aud.currentTime % 60);
    if (secs < 10) {
      secs = "0" + String(secs);
    }

    $("#timestamp").text(mins + ":" + secs);
  };
}
.player {
  position: relative;
  display: flex;
  border-radius: 0px;
  padding: 5 16px;
}

.player .info {
  display: flex;
  flex-flow: column nowrap;
  justify-content: center;
  width: 50%;
  padding: 0 16px;
}

.player .info .name {
  font-size: 15px;
  font-weight: 700;
}

.player .info .singer {
  font-size: 12px;
}

.player .audioBtns {
  width: 50%;
  /*align-items: center;*/
  padding: 0 16px;
}

.player .audioBtns div:nth-child(1) {
  font-size: 30px;
}

.player .audioBtns div:nth-child(2),
.player .audioBtns div:nth-child(3) {
  font-size: 18px;
}

.player .progress {
  position: absolute;
  height: 5px;
  left: 0;
  bottom: 0;
  background-color: #f8f9fa;
  border-radius: 0px;
}

.icon-loop {
  content: url("loop.svg");
  width: 20px;
  height: 20px;
  display: block;
}

.icon-play:before {
  content: url("https://image.flaticon.com/icons/svg/860/860768.svg");
  width: 30px;
  height: 45px;
  display: block;
}

.icon-stop:before {
  content: url("https://image.flaticon.com/icons/svg/633/633940.svg");
  width: 30px !important;
  height: 40px !important;
  display: block;
}

.playBtn {
  padding: 0 16px;
}

.activeOverlay {
  filter: invert(42%) sepia(11%) saturate(1216%) hue-rotate(279deg)
    brightness(88%) contrast(85%);
}

.audioContainer {
  background-color: #d3d3d3;
  padding: 2% 2% !important;
}

.audioContainer:not(:last-child) {
  margin-bottom: 3%;
}

.audioContainerActive {
  background-color: #855b6f !important;
  color: #f8f9fa !important;
}

.IconActive {
  filter: invert(92%) sepia(17%) saturate(3434%) hue-rotate(186deg)
    brightness(127%) contrast(95%);
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- First Audio Container. OnClick on Line 5 -->
<div class="row no-gutters d-flex flex-column col-12">
  <div class="col d-flex flex-column justify-content-xl-center audioContainer">
    <div class="player">
      <div class="d-flex flex-row align-items-xl-center playBtn" onclick="playAudio(1)"><i class="fas fa-pause iconfont play-pause icon-play"></i></div><audio src="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3" type="audio/mpeg"></audio>
      <div class="info">
        <div class="name">
          <p id="qwewq">By The Shell</p>
        </div>
        <div class="singer">
          <p>Adam M.</p>
        </div>
      </div>
      <div class="d-flex flex-row justify-content-around justify-content-xl-end align-items-xl-center audioBtns">
        <p id="timestamp" style="font-size: 20px;margin-bottom: 0px;">00:00</p>
      </div>
      <div data-toggle="tooltip" id="progressBar" class="progress"></div>
    </div>
  </div>

  <!-- Second Audio Container. OnClick on Line 25 -->

  <div class="col d-flex flex-column justify-content-xl-center audioContainer">
    <div class="player">
      <div class="d-flex flex-row align-items-xl-center playBtn" onclick="playAudio(2)"><i class="fas fa-pause iconfont play-pause icon-play"></i></div><audio src="https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3" type="audio/mpeg"></audio>
      <div class="info">
        <div class="name">
          <p id="qwewq-1">Turntable</p>
        </div>
        <div class="singer">
          <p>Adam M.</p>
        </div>
      </div>
      <div class="d-flex flex-row justify-content-around justify-content-xl-end align-items-xl-center audioBtns">
        <p id="timestamp-1" style="font-size: 20px;margin-bottom: 0px;">00:00</p>
      </div>
      <div data-toggle="tooltip" id="progressBar-1" class="progress"></div>
    </div>
  </div>
</div>

1 Answers
Related