manoj
manoj

Reputation: 121

jQuery animating all elements instead of the next one only

I was trying to apply animation to the .typeform element when I click the .nexto element. When using toggleClass all the .typeform were affected by the toggleClass.

I need to apply the animation to the next .typeform element when I click the .nexto. Please help me on this. Thanks.

$('.nexto').on('click', function(event) {
  $('.typeform-element').toggleClass('animations');
})
.animations {
  animation: animation-1 1s linear;
}

@keyframes animation-1 {
  0% {
    top: -50px;
    opacity: 0;
  }
  50% {
    top: -25px;
    opacity: 0.5;
  }
  100% {
    top: 0px;
    opacity: 1;
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="typeform">
  <div class="typeform-element" id="fname">
    <p><span class="list">1</span>&#8594;What's your First Name? *</p>
    <input type="text" name="fname" placeholder="Type your answer..." autofocus>
    <div class="ok-btn">
      <a href="#lname" class="nexto">OK &#10004;</a>
    </div>
  </div>
  <div class="typeform-element" id="lname">
    <p><span class="list">2</span>&#8594;What's Your Last Name? *</p>
    <input type="text" name="lname" placeholder="Type your answer...">
    <div class="ok-btn">
      <a href="#email" class="nexto">OK	&#10004;</a>
    </div>
  </div>
  <div class="typeform-element" id="email">
    <p><span class="list">3</span>&#8594;Let's know your Email Address*</p>
    <input type="email" name="email" placeholder="Type your answer...">
    <div class="ok-btn">
      <a href="#submit" class="nexto">OK &#10004;</a>
    </div>
  </div>
  <div class="typeform-element" id="submit">
    <div class="ok-btn">
      <a href="#fname">Submit &#10004;</a>
    </div>
  </div>
</div>

Upvotes: 2

Views: 46

Answers (1)

Mahatmasamatman
Mahatmasamatman

Reputation: 1535

You need to access your buttons parent, and then select the immediately following sibling, and toggle his class only.

$('.nexto').on('click', function(event){
           $(this).closest('.typeform-element').next('.typeform-element').toggleClass('animations');
    });

Upvotes: 3

Related Questions