1

I'm trying to change the class of an html element i using jquery. What i'm trying to achieve is when the user clicks on div , I would like the CSS class of the i under the clicked div to change.

So far I'm unable to achieve this. Any tip will be appreciated.

$(document).ready(function() {
  $('div.gen').click(function() {
    $(this).next('span i').toggleClass('fa-angle-double-up fa-angle-double-down');
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<li>
  <div class="gen"><span>
         <div class="inline-block"> 👨
           <figcaption>John</figcaption>
         </div>

         <i class=" stack-top box arrow bounce fa fa-angle-double-down" style="font-size:24px"></i>
         <div class="inline-block"> 👩
           <figcaption>Jane</figcaption>
         </div>
       </span>
  </div>
</li>
<hr>
<li>
  <div class="gen"><span>
         <div class="inline-block"> 👨
           <figcaption>John2</figcaption>
         </div>

         <i class=" stack-top box arrow bounce fa fa-angle-double-down" style="font-size:24px"></i>
         <div class="inline-block"> 👩
           <figcaption>Jane2</figcaption>
         </div>
       </span>
  </div>
</li>

Thanks

2 Answers 2

2

The find method should do the trick instead of next since find traverses down the DOM

 $(this).find('span i').toggleClass('fa-angle-double-up fa-angle-double-down');
Sign up to request clarification or add additional context in comments.

Comments

0

You can use jQuery's find, as following:

$(document).ready(function() {
  $('div.gen').click(function() {
     $(this).find('span i').toggleClass('fa-angle-double-up fa-angle-double-down');
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<li>
  <div class="gen"><span>
         <div class="inline-block"> 👨
           <figcaption>John</figcaption>
         </div>

         <i class=" stack-top box arrow bounce fa fa-angle-double-down" style="font-size:24px"></i>
         <div class="inline-block"> 👩
           <figcaption>Jane</figcaption>
         </div>
       </span>
  </div>
</li>
<hr>
<li>
  <div class="gen"><span>
         <div class="inline-block"> 👨
           <figcaption>John2</figcaption>
         </div>

         <i class=" stack-top box arrow bounce fa fa-angle-double-down" style="font-size:24px"></i>
         <div class="inline-block"> 👩
           <figcaption>Jane2</figcaption>
         </div>
       </span>
  </div>
</li>

Comments

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.