1

I have a number of divs with the same season-list class and each has a data-episode-count data attribute. I need to be able to grab the attribute on click and use that value to hide js-show-more-trigger if the value of the attribute is greater than 6. I'm currently looping through all season-list classes, but not sure how to grab the data attribute from the div:

HTML

<div class="js-season-list-item" id="season-5" style="display: block;">
  <div class="season-list" data-episode-count="3">
    <div class="season-list__item">
      <div class="episode-item">
        <div class="episode-card js-episode-card">
          <div class="episode-card__overlay"><a href="/play/3099013"><span class="play-circle sm" data-play-path="/play/3099013"><svg class="svg svg-play"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/svg/svg-defs.svg#svg-play">svg-play</use></svg></span></a></div>
        </div>
        <div class="episode-details">
          <h1 class="heading md hvy">Episode 1</h1>
          <p></p>
          <p class="runtime">21min</p>
        </div>
      </div>
    </div>
    <div class="season-list__item">
      <div class="episode-item">
        <div class="episode-card js-episode-card">
          <div class="episode-card__overlay"><a href="/play/3099014"><span class="play-circle sm" data-play-path="/play/3099014"><svg class="svg svg-play"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/svg/svg-defs.svg#svg-play">svg-play</use></svg></span></a></div>
        </div>
        <div class="episode-details">
          <h1 class="heading md hvy">Episode 2</h1>
          <p></p>
          <p class="runtime">21min</p>
        </div>
      </div>
    </div>
    <div class="season-list__item">
      <div class="episode-item">
        <div class="episode-card js-episode-card">
          <div class="episode-card__overlay"><a href="/play/3099015"><span class="play-circle sm" data-play-path="/play/3099015"><svg class="svg svg-play"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/svg/svg-defs.svg#svg-play">svg-play</use></svg></span></a></div>
        </div>
        <div class="episode-details">
          <h1 class="heading md hvy">Episode 3</h1>
          <p></p>
          <p class="runtime">21min</p>
        </div>
      </div>
    </div>
  </div>
</div>

JavaScript

let trigger = document.getElementsByClassName('js-show-more-trigger');
let seasonList = document.getElementsByClassName("season-list")
for(let i = 0; i < seasonList.length; i++) {
  if(seasonList[i].getAttribute('data-episode-count') < 6){
    trigger.style.display = "none";
  }
}

Codepen: Link

4
  • 1
    seasonList[i].dataset.episodeCount Commented Mar 21, 2018 at 21:52
  • console.log(seasonList[i].getAttribute('data-episode-count')) Commented Mar 21, 2018 at 21:53
  • Four identical answers within a few minutes... hmm. Commented Mar 21, 2018 at 21:58
  • Everyone is chomping at the bit, haha. Commented Mar 21, 2018 at 21:59

3 Answers 3

1

You can get data in Javascript by dataset property. In your case:

let seasonList = document.getElementsByClassName("season-list")
for(let i = 0; i < seasonList.length; i++) {
  console.log(seasonList[i].dataset.episodeCount)
}
Sign up to request clarification or add additional context in comments.

Comments

1

With Element.getAttribute()

seasonList[i].getAttribute('data-episode-count');

With HTMLElement.dataset

seasonList[i].dataset.episodeCount;

Comments

1

First and foremost you can check the documentation for better understating.

If I understand you correctly, you want to hide the "Show More" link when the episodeCount > 6, the snippet below is working as intended. Your ifstatement was only hiding the trigger if the value was less then 6.

You can check the behavior by changing the data-episode-count value.

let trigger = document.getElementsByClassName('js-show-more-trigger');
let seasonList = document.getElementsByClassName("season-list")
for(let i = 0; i < seasonList.length; i++) {
  if(seasonList[i].dataset.episodeCount > 6){
    trigger[i].style.display = "none";
  }
}
.season-list{
  display: flex;
}
<div class="js-season-list-item" id="season-5" style="display: block;">
  <div class="season-list" data-episode-count="7">
    <div class="season-list__item">
      <div class="episode-item">
        <div class="episode-card js-episode-card">
          <div class="episode-card__overlay"><a href="/play/3099013"><span class="play-circle sm" data-play-path="/play/3099013"><svg class="svg svg-play"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/svg/svg-defs.svg#svg-play">svg-play</use></svg></span></a></div>
        </div>
        <div class="episode-details">
          <h1 class="heading md hvy">Episode 1</h1>
          <p></p>
          <p class="runtime">21min</p>
        </div>
      </div>
    </div>
    <div class="season-list__item">
      <div class="episode-item">
        <div class="episode-card js-episode-card">
          <div class="episode-card__overlay"><a href="/play/3099014"><span class="play-circle sm" data-play-path="/play/3099014"><svg class="svg svg-play"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/svg/svg-defs.svg#svg-play">svg-play</use></svg></span></a></div>
        </div>
        <div class="episode-details">
          <h1 class="heading md hvy">Episode 2</h1>
          <p></p>
          <p class="runtime">21min</p>
        </div>
      </div>
    </div>
    <div class="season-list__item">
      <div class="episode-item">
        <div class="episode-card js-episode-card">
          <div class="episode-card__overlay"><a href="/play/3099015"><span class="play-circle sm" data-play-path="/play/3099015"><svg class="svg svg-play"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/svg/svg-defs.svg#svg-play">svg-play</use></svg></span></a></div>
        </div>
        <div class="episode-details">
          <h1 class="heading md hvy">Episode 3</h1>
          <p></p>
          <p class="runtime">21min</p>
        </div>
      </div>
    </div>
  </div>
</div>

<div class="show-more"><span class="js-show-more-trigger">Show 
More</span></div>

1 Comment

This is a good start and gets all the available data-episode-count, but how would I target one specifically if there are multiple season-list divs on the page?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.