Change label class (icon-font) with click

1

I'm using iconfonts in a drop-down menu, where the link looks like this:

<li class="abre-dropdown"><a href="javascript:void(0)"><label style="float:left;" class="icon-location"></label>Nossa casa<label class="icon-down-dir"></label></a></li>

Is it possible, when the menu is active (toggle effect), to change its class from icon-down-dir to any other, such as icon-up-dir ?

jquery script to open / close the menu:

var $j = jQuery.noConflict();
$j(".abre-dropdown").click(function(){
    $j(this).children("ul").slideToggle();
})
$j("ul").click(function(p){
    p.stopPropagation();
})

    
asked by anonymous 05.01.2016 / 17:17

2 answers

1

Here's a solution to your problem:

var $ = jQuery.noConflict();
$("nav li.abre-dropdown > a").on('click', function() {
  var $label = $(this).parent().find('> a > .icon-down-dir, > a > .icon-up-dir');
  var $isdown = $label.is('.icon-down-dir');
  $label
    .removeClass()
    .addClass($isdown ? 'icon-up-dir' : 'icon-down-dir')
});
/* Remover esse reset adicionar pra facilitar a visualização */

* {
  margin: 0;
  padding: 0;
  line-height: 1;
  font-family: sans-serif;
}
.menuMobile ul li {
  border-top: 1px solid #38393f;
  background: #25262a;
}
.menuMobile ul li a {
  color: #fff;
  display: block;
  padding: 15px 0 15px 10px;
}
.menuMobile ul li a label {
  float: right;
}
.menuMobile ul li a:hover {
  color: deepskyblue;
  transition: color 0.5s;
}
/* Remover so serve para pintar oque seria o icone */

.icon-down-dir {
  background: red;
  display: inline-block;
  width: 15px;
  height: 15px;
}
.icon-up-dir {
  background: blue;
  display: inline-block;
  width: 15px;
  height: 15px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><navclass="menuMobile">
  <li class="abre-dropdown">
    <a href="javascript:void(0)">
      <label style="float:left;" class="icon-location"></label>Nossa casa
      <label class="icon-down-dir"></label>
    </a>
    <ul class="submenu-1">
      <li class="abre-dropdown">
        <a href="javascript:void(0)">
          <label style="float:left;" class="icon-home"></label>Sobre nós
          <label class="icon-down-dir"></label>
        </a>
        <ul class="submenu-2">
          <li><a href="#terreiro">A casa</a>
          </li>
          <li><a href="#mae-maria">Mãe Maria C.</a>
          </li>
          <li><a href="#ogans">Ogans</a>
          </li>
          <li><a href="#filhos">Filhos</a>
          </li>
          <li><a href="#festas">Festas</a>
          </li>
        </ul>
      </li>
      <li>
        <a href="http://<?php echo $url; ?>/agenda">
          <label style="float:left;" class="icon-calendar"></label>Agenda</a>
      </li>
      <li>
        <a href="http://<?php echo $url; ?>/localizacao">
          <label style="float:left;" class="icon-location"></label>Localização</a>
      </li>
      <li>
        <a href="http://<?php echo $url; ?>/contato">
          <label style="float:left;" class="icon-phone"></label>Contato</a>
      </li>
    </ul>
  </li>
</nav>
    
06.01.2016 / 20:17
3

Use the jquery .toggleClass ()

$(".abre-dropdown").click(function(){
    $(this).toggleClass(function() {
      if ( $( this ).hasClass("icon-down-dir") ) {
        return "icon-up-dir";
      } else {
        return "icon-down-dir";
      }
    });
});

It would look like this, I set an example for you in the jsFiddler .

    
05.01.2016 / 17:20