linnse
linnse

Reputation: 461

Using jQuery 1.12.4 to rotate chevron 180 degrees on click

I'm having trouble figuring out what should be a simple task, but cannot seem to rotate this chevron 180 degrees on click. Our RTE converts <i> tags to <em>, and we're using an older version of jQuery (1.12.4), but I don't think those are causing the issue. I've tried a variety of scripts and have landed on this so far. Can anyone see what I'm missing?:

$(document).ready(function() {
    $('#view-more-cards').click(function() {
       $('.smb-chevron-container > a > em > svg').css('transform', 'rotate(180deg)');
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>


<div class="container">
  <div class="smb-chevron-container" id="view-more-cards">
    <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" data-target="#view-more">View all internet solutions
      <em>
        <svg xmlns="http://www.w3.org/2000/svg" id="view-more-chevron" width="14.223" height="7.111" viewbox="0 0 14.223 7.111">
          <g id="Ren_Icons_Controls_add-maximize" data-name="Ren/Icons/Controls/add-maximize">
            <path id="smb-chevron" d="M7.111,7.111a.789.789,0,0,1-.535-.2L.219,1.158A.634.634,0,0,1,.219.2a.806.806,0,0,1,1.06,0l5.832,5.28L12.943.2A.806.806,0,0,1,14,.2a.634.634,0,0,1,0,.959L7.646,6.912a.789.789,0,0,1-.535.2Z" fill="#0057b8"></path>
          </g>
        </svg>
      </em>
    </a>
  </div>
</div>

<div class="collapse" id="view-more">
  Hello World
</div>

Demo: https://jsfiddle.net/Codewalker/5su8029z/4/

Upvotes: 1

Views: 357

Answers (1)

MaxiGui
MaxiGui

Reputation: 6358

I just add the condition in your jQuery. The goal is to check if the element is expanded and if it is, it will set back rotation to 0deg.

if($('.smb-chevron-container > a').attr("aria-expanded") == "true"){
  $('.smb-chevron-container > a > em > svg').css('transform', 'rotate(0deg)');
}else{
  $('.smb-chevron-container > a > em > svg').css('transform', 'rotate(180deg)');
}
       

DEMO

$(document).ready(function() {
    $('#view-more-cards').click(function() {
      if($('.smb-chevron-container > a').attr("aria-expanded") == "true"){
        $('.smb-chevron-container > a > em > svg').css('transform', 'rotate(0deg)');
      }else{
        $('.smb-chevron-container > a > em > svg').css('transform', 'rotate(180deg)');
      }
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>


<div class="container">
  <div class="smb-chevron-container" id="view-more-cards">
    <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" data-target="#view-more">View all internet solutions
      <em>
        <svg xmlns="http://www.w3.org/2000/svg" id="view-more-chevron" width="14.223" height="7.111" viewbox="0 0 14.223 7.111">
          <g id="Ren_Icons_Controls_add-maximize" data-name="Ren/Icons/Controls/add-maximize">
            <path id="smb-chevron" d="M7.111,7.111a.789.789,0,0,1-.535-.2L.219,1.158A.634.634,0,0,1,.219.2a.806.806,0,0,1,1.06,0l5.832,5.28L12.943.2A.806.806,0,0,1,14,.2a.634.634,0,0,1,0,.959L7.646,6.912a.789.789,0,0,1-.535.2Z" fill="#0057b8"></path>
          </g>
        </svg>
      </em>
    </a>
  </div>
</div>

<div class="collapse" id="view-more">
  Hello World
</div>

Upvotes: 2

Related Questions