All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript Toggle Class

Last Updated : Mar 11, 2024

JavaScript Toggle Class

In this article we will show you the solution of JavaScript toggle class, toggle is a feature of JavaScript that is used as the default approach for dealing with the hide() & show() functions for the selected items.

The technique uses the show() method to display the components and the hide() method to hide the elements while checking the visibility of the specific selected elements in the meantime.

When it is in the visible state, that effect is also referred to as a toggle effect.

In all of javascript, the div> tag is used to add and remove class names from elements, and the id attribute is used to add a toggle class name. Now move to the conept of javascript toggle class.

Step By Step Guide On JavaScript Toggle Class :-

When we add and remove one or many class names from the necessary selected items, we toggle using the toggleClass() method.

The class names were only ever added if the components are missing as well as they have already been eliminated inside this set toggleClass() method, which is the same one used by the jquery javascript library.

By using the toggle() function of the DOMTokenList object in JavaScript or the contains(), add(), and remove() methods of the object's DOMTokenList object, a class can be dynamically added to an element if one has not yet been assigned to it, or it can be dynamically deleted from an element if one has already been present.

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Talkerscode</title>
 <style>
  .paragraphClass {
   font-size: 100px;
   color: grey;
  }
  #Button {
   margin-top: 15px;
  }
 </style>
 <script>
  function myFunc() {
   var para = document.getElementById("p");
   para.classList.toggle("paragraphClass");
  }
 </script>
</head>
<body>
 <p id="p">
  Welcome to Talkerscode
 </p>
 <button id="Button" onclick="myFunc()">
  Click Me
 </button>
</body>
</html>
  1. We begin our code with the tags <!DOCTYPE html>, HTML, and HEAD, where we employ the meta tag.
  2. The HTML document's character encoding is specified by the Meta charset tag's charset attribute.
  3. Next, we use the TITLE tag to give the webpage a title before beginning the css style using the STYLE tag.
  4. For the paragraphclass and the button, we use a grey colour and a font size in pixels, along with a top margin in pixels.
  5. Next, we use the SCRIPT element to start the script after finishing the style.
  6. We write a function in script and declare a variable with the Document method getElementById(), which returns an Element object corresponding to the element whose id field matches the given string.
  7. Next, we create a toggling effect for the paragraphclass using the toggleClass function.
  8. After that, using the BODY tag, we finish the script and begin the body of the code.
  9. To make a statement that we want to display with a toggle effect, we use a P tag in the body and give it an id.
  10. After that, we construct a button with a onclick event attached, meaning that code will run when the user clicks the button.
  11. Finally, BODY & HTML mark the end of our code.

Conclusion :-

Thus, we were able to understand the javascript toggle class. Also, we discovered that toggle is a crucial component of JavaScript since it's frequently utilised in scenarios from the perspective of the user.

When a user needs to see certain data on a page, it will conceal that data.

It works with all types of browsers.

I hope this article on JavaScript toggle class helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪