jQuery addClass example

Change class name on click in jQuery

by sazakharov

HTML

<nav class="cbp-spmenu cbp-spmenu-vertical-min cbp-spmenu-left cbp-spmenu-open" id="showLeftMenu" data-active="1">
   <div class="showMenuButton" id="showLeftMenuTrigger" data-trigger="leftSlide" data-idclick="showLeftMenu" title="Нажмите чтобы открыть/скрыть иеню сайта">
    <div class="bar"></div>
    <div class="bar"></div>
    <div class="bar"></div>
   </div>
   </nav>
   
   <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
   <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
   <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing...

CSS

.cbp-spmenu {
	height: 100%;
	position: fixed;
    overflow-x: hidden;
    overflow-y: auto;
    background-color: #2b3a41;
	box-shadow: 7px 10px 23px -4px #00d2ff;
    }


.cbp-spmenu-vertical {
	width: 220px;
	height: 100%;
	z-index: 1000;
}

.cbp-spmenu-vertical-min {
	width: 220px;
	height: 100%;
	z-index: 1000;
}

.cbp-spmenu-left {
	left: -222px;
}


.cbp-spmenu-left.cbp-spmenu-open {
	left: 0px;
}


.cbp-spmenu-push {
	overflow-x: hidden;
	position: relative;
	left: 0;
}

.cbp-spmenu-push-toleft {
	left: -200px;
}


.cbp-spmenu,
.cbp-spmenu-push {
	-webkit-transition: all 0.3s ease;
	-moz-transition: all 0.3s ease;
	transition: all 0.3s ease;
}

.bar {
width: 18px;
margin: 2px;
border: 1px solid #fff;

}

.showMenuButton {
  position: fixed;
  top: 25px;
  left: 225px;
  cursor: pointer;
  padding: 10px 10px 5px 5px;
  width: 20px;
  height: 20px;
  background-color: #35b1f0;
  border-radius: 4px;
  z-index: 1001;
  -webkit-transition: all 0.3s ease;
  -moz-transition: all 0.3s ease;
  transition: all 0.3s ease;
}

.menuButtonShift {
  left: 5px;
}

JavaScript

jQuery(document).ready(function ($)  {

   $('[data-trigger="leftSlide"]').click(function() {
  // Получим ID элемента
  var thisID = $(this).attr('id');
  // Получим значение ID элемента меню, с которым будем работать
  var idClick = $('#'+thisID).attr('data-idClick');

   elemThis = document.getElementById(thisID);
   elemThisClick = document.getElementById(idClick);
   body = document.body;

				var active = $('#'+idClick).attr('data-active');
				// Меню оказалось открыто
				if(active == '1'){
	            // Его нужно скрыть
				$('#'+idClick).removeClass('cbp-spmenu-open' );
				// И сменить состояние на закрытое
				$('#'+idClick).attr('data-active', '0');
        $('.showMenuButton').addClass('menuButtonShift');
				// Меню еще не открыто
				}else{
				// Меняем статус открытых уже меню на скрытые
				$(".cbp-spmenu-open").attr('data-active', '0');
				// Скрываем уже открытые меню
				$(".cbp-spmenu-open").removeClass('cbp-spmenu-open' );
				// открываем меню, которое запросили
				$("#"+idClick).addClass('cbp-spmenu-open' );
        $('.showMenuButton').removeClass('menuButtonShift');
				// Ставим ему статус как открытое
				$('#'+idClick).attr('data-active', '1');
				}

})

   });