M
M
miletskij2016-09-08 14:30:19
symfony
miletskij, 2016-09-08 14:30:19

How to animate a child class of the same type of blocks?

Good afternoon, there are three blocks of the same type:

<div class="question_block">
    <div class="question_block__question">
    </div>
    <div class="question_block__answer">
    </div>
</div>


The .question_block__answer block is hidden($('.question_block__answer').hide();) and opens when the .question_block__question block is clicked.

In an attempt to make the code more compact and not prescribe jQuery for each such .question_block block, I try to get the animation to run for a specific block by assigning the active class, but alas.
Please tell me how to implement.

Here is the function I wrote:
// тут я присваиваю нажатому блоку класс active
  $('.question_block').click(function(q) {
  q.preventDefault();
  $(this).toggleClass('active');
});
// здесь и начинается... 
$('.question_block').click(function(q) {// при нажатии на активный блок...
  if($('.question_block__answer').is(':hidden')){//...проверяется скрыт ли блок ответа...
    $('.question_block__answer').fadeIn(600);}//...если да - всплываем его...
  else{$('.question_block__answer').fadeOut(600);}//если нет - гасим
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Cheremisin, 2016-09-08
@miletskij

$('.question_block').click(function(q) {
   var $answer = $(this).parent().find('.question_block__answer');
   if($answer.is(':hidden')){
    .....
   }
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question