Tab content on jQuery

Viewed 36

I'm a beginner front-end development. I'm from Ukraine and my English is so so... but I learning English again and again.
So I'll go on...

How can I make that when I click on a new block, the previous one is closed? enter image description here

I have this code

<pre>
<div class="gallery__column">
 <div class="gallery__item">
    <div class="overlay gallery__overlay"></div>                    
        <div class="gallery__image">
            <a class="content-open" href="##">
            <img class="gallery__img" src="img/example/01.jpg" alt="">
            <span data-tab="tab-4" class="gallery__arrow arrow"></span>
            </a>
        </div>
    <div class="gallery__content" id="tab-4">
    <h4 class="gallery__title">Электрические шторы</h4>
    <div class="gallery__brand">SOMFY 1</div>
    <div class="gallery__text"></div>
    </div>
    </div>
</div>
</pre>

<div class="gallery__item">
<div class="overlay gallery__overlay"></div>
<div class="gallery__image">
<img class="gallery__img" src="img/example/02.jpg" alt="">
    <a class="content-link" href="##"></a>
    <span data-tab="tab-1" class="gallery__arrow arrow"></span>
    </div>
    <div class="gallery__content" id="tab-1">
        <h4 class="gallery__title">Электрические шторы</h4>
        <div class="gallery__brand">SOMFY 1</div>
        <div class="gallery__text">
</div>
</div>
$('.arrow').on('click', function(){
        $('.arrow').removeClass('active');
        $('.arrow').parents().siblings('.gallery__content');
        $(this).addClass('active');
        $(this).parents().siblings('.gallery__content').slideToggle();
    });
2 Answers

I dont know if I understood all of your requirments, but from what I understood I tried to make this:

$(document).ready(function() {
  $('.button').click(function() {
  $(".box").hide();
    $(this).siblings(".box").toggle();
  });
});
.box {
  display: none;
}
  <script src="https://code.jquery.com/jquery-3.5.0.js"></script>

<div class="container" style="width:30%;display:inline-block;">
  <button class="button">Hide/Show</button>
  <div class="head" style="background:grey;">front page</div>
  <div class="box" style="background:lightgrey;">
    This is a paragraph with little content. This is another small paragraph.
  </div>
</div>

<div class="container" style="width:30%;display:inline-block;">
  <button class="button">Hide/Show</button>
  <div class="head" style="background:grey;">front page</div>
  <div class="box" style="background:lightgrey;">
    This is a paragraph with little content. This is another small paragraph.
  </div>
</div>

<div class="container" style="width:30%;display:inline-block;">
  <button class="button">Hide/Show</button>
  <div class="head" style="background:grey;">front page</div>
  <div class="box" style="background:lightgrey;">
    This is a paragraph with little content. This is another small paragraph.
  </div>
</div>

You almost had it, but I would recommend that you use .closest() combinded with .find() in your case like the example below.

$('.arrow').on('click', function() {
  $('.arrow').removeClass('active');
  $('.arrow').not(this).closest('.gallery__item').find('.gallery__content');
  $(this).addClass('active');
  $(this).closest('.gallery__item').find('.gallery__content').slideToggle();
});

Demo

$('.arrow').on('click', function() {
  $('.arrow').removeClass('active');
  $('.arrow').not(this).closest('.gallery__item').find('.gallery__content').hide();
  $(this).addClass('active');
  $(this).closest('.gallery__item').find('.gallery__content').slideToggle();
});
.gallery__content {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<pre>
<div class="gallery__column">
 <div class="gallery__item">
    <div class="overlay gallery__overlay"></div>                    
        <div class="gallery__image">
            <a class="content-open" href="##">
            <img class="gallery__img" src="img/example/04.jpg" alt="">
            <span data-tab="tab-4" class="gallery__arrow arrow">arrow</span>
            </a>
        </div>
    <div class="gallery__content" id="tab-4">
    <h4 class="gallery__title">Электрические шторы</h4>
    <div class="gallery__brand">SOMFY 1</div>
    <div class="gallery__text"></div>
    </div>
    </div>
</div>
</pre>



<pre>
    <div class="gallery__column">
     <div class="gallery__item">
        <div class="overlay gallery__overlay"></div>                    
            <div class="gallery__image">
                <a class="content-open" href="##">
                <img class="gallery__img" src="img/example/04.jpg" alt="">
                <span data-tab="tab-4" class="gallery__arrow arrow">arrow</span>
                </a>
            </div>
        <div class="gallery__content" id="tab-4">
        <h4 class="gallery__title">Электрические шторы</h4>
        <div class="gallery__brand">SOMFY 1</div>
        <div class="gallery__text"></div>
        </div>
        </div>
    </div>
    </pre>

Related