Toggle hide class only on selected div with JavaScript?

To toggle hide class only on selected div, you need to set event on click button. Let’s say you need to hide a specific div on the click of + sign.

To get font + or - icon, you need to link font-awesome −

<link rel="stylesheet" href="

Following is the code to toggle hide class on clicking + sign −


 Live Demo

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="//">
<script src=""></script>
<script src=""></script>
<link rel="stylesheet" href="
.hideDiv {
display: none;
<div class="firstDetails">
<h2 class="customer-track">Customer 1<i class="fa fa-plus"></i></h2>
<p class="customer">John</p>
<p class="customer">US</p>
<div class="secondDetails">
<h2 class="customer-track">Customer 2 <i class="fa fa-plus"></i></h2>
<p class="customer">David</p>
<p class="customer">AUS</p>
$(".fa-plus").on("click", function(){

To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.


This will produce the following output −

After clicking the plus icon besides “Customer1”, you will get the following output i.e. Customer2 div will vanish −

Updated on: 11-Sep-2020


Kickstart Your Career

Get certified by completing the course

Get Started