-1

I have a page on my site where I display one block of content & give the option for the user to click 'See more' to reveal more.

Here's my current demo.

HTML:

<p>
<a href="#" id="category36SeeMore">See more</a>
</p>

<div class="category36">
test
</div>

<div class="category36" style="display:none;">
test
</div>

<div class="category36" style="display:none;">
test
</div>

<div class="category36" style="display:none;">
test
</div>

Can someone give me some pointers in how to create a function for #category36SeeMore so it shows ALL divs with the class category36?

michaelmcgurk
  • 6,367
  • 23
  • 94
  • 190
  • Does this answer your question? [Make text appear/disappear on button click](https://stackoverflow.com/questions/55442477/make-text-appear-disappear-on-button-click) – teddcp Apr 09 '20 at 14:34
  • @tedd Thanks for the reply. Is it possible to apply this so it hits ALL div elements with the class name? – michaelmcgurk Apr 09 '20 at 14:36

2 Answers2

2

On category36SeeMore click, you can first get its id and then replace SeeMore from it like:

const id = this.id.replace('SeeMore', '')

So, that you can get the actual class name category36 from it. Then you can find all elements with that class using:

document.querySelectorAll('.' + id)

and finally using .forEach, change the display style for all elements to block to make all of them visible again.

Demo:

const btn = document.getElementById("category36SeeMore");
btn.addEventListener("click", function(e){
  e.preventDefault();
  const id = this.id.replace('SeeMore', '')
  document.querySelectorAll('.' + id).forEach(el=>el.style.display = 'block')
});
.category36 {
  margin: 5px;
  padding: .5rem;
  box-shadow: 2px 2px 5px rgba(0,0,0,0.03);
  border-radius: 4px;
  background: Skyblue;
  border-bottom: 1px solid #F9F2D6;
  border-right: 1px solid #F9F2D6;
}
<p>
<a href="#" id="category36SeeMore">See more</a>
</p>

<div class="category36">
test
</div>

<div class="category36" style="display:none;">
test
</div>

<div class="category36" style="display:none;">
test
</div>

<div class="category36" style="display:none;">
test
</div>
palaѕн
  • 72,112
  • 17
  • 116
  • 136
2

You can use jquery.

<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script>
$(document).ready(function(){
    $("#category36SeeMore").click(function(){
        $(".category36").css('display','block');
    });
});
</script>