"Add remove class on click using jQuery"
Bootstrap 4.1.1 Snippet by sunil8107

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>btn ADD REMOVE CLASS</title>
</head>
<body>
<div class="main_div">
<button type="button" class="btn">Button</button>
<hr>
<div class="box">
<h2>Lorem Ipsum dolor sit amet</h2>
</div></div>
<p class="text-center top_spac"> Create by <a target="_blank" href="https://in.linkedin.com/in/sunil-rajput-nattho-singh">Sunil Rajput</a></p>
</body>
</html>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
2
3
4
.main_div{text-align:center; padding: 20px;}
.box{ display:none;}
.box.current{ display:block;}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$(document).ready(function(){
$('.btn').on('click',function(){
if($('.box').hasClass('current')){
$('.box').removeClass('current');
}else{
$('.box').addClass('current');
}
});
});
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Related: See More


Questions / Comments: