Answers for "how to toggle class of parent in javascript"

1

how to toggle class of parent in javascript

/* HTML */

/*
<div class="tab-links closed">
	<a class="tab-links__link" onclick="toggleLink(this)"></a>
    <a class="tab-links__link" onclick="toggleLink(this)"></a>
    <a class="tab-links__link" onclick="toggleLink(this)"></a>
    <a class="tab-links__link" onclick="toggleLink(this)"></a>
</div>
*/

const toggleLink = (element) => {
 element.parentNode.classList.toggle('closed');
}
Posted by: Guest on January-13-2021
-1

how to toggle class in children element

/* HTML
<div id=”menuIcon” onclick=”menuAnimate()”>
  <div class=”bar”></div>
  <div class=”bar”></div>
  <div class=”bar”></div>
</div>
*/

// Javascript
 menuAnimate = () => {
  const main = document.getElementById('menuIcon');
  main.classList.toggle('rotate');
  let c = document.getElementById('menuIcon').children;
  c[0].classList.toggle('rotateL');
  c[1].classList.toggle('slick');
  c[2].classList.toggle('rotateR');
}
Posted by: Guest on January-04-2021

Code answers related to "how to toggle class of parent in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language