Skip to content

Instantly share code, notes, and snippets.

@shibbirweb
Last active January 2, 2019 12:36
Show Gist options
  • Save shibbirweb/4023b0e5e3177c17d35971776389a3cf to your computer and use it in GitHub Desktop.
Save shibbirweb/4023b0e5e3177c17d35971776389a3cf to your computer and use it in GitHub Desktop.
Add and Remove Class in jQuery
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<!-- Ionicons CSS -->
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.0/css/ionicons.css">
<title>Class remove and add</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-8 offset-md-2">
<div class="side-bar">
<div class="search-item">
<div class="item-header" id="deadline-filter">
<a href="#" data-toggle="collapse" data-target="#deadline" aria-expanded="true" aria-controls="deadline">
Deadline
<i class="ion-ios-arrow-down myarrow"></i> <!-- here class will change -->
</a> <!-- Event happen here -->
</div>
<div id="deadline" class="collapse show" aria-labelledby="deadline-filter">
<div class="item-body">
<form action="#" method="POST">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="deadline1">
<label class="custom-control-label" for="deadline1">All</label>
<span>(4,121)</span>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="deadline2">
<label class="custom-control-label" for="deadline2">1 day left</label>
<span>(4,121)</span>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="deadline3">
<label class="custom-control-label" for="deadline3">2 day left</label>
<span>(4,121)</span>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="deadline4">
<label class="custom-control-label" for="deadline4">3 day left</label>
<span>(4,121)</span>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="deadline5">
<label class="custom-control-label" for="deadline5">4 day left</label>
<span>(4,121)</span>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="deadline6">
<label class="custom-control-label" for="deadline6">5 day left</label>
<span>(4,121)</span>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="deadline7">
<label class="custom-control-label" for="deadline7">6 day left</label>
<span>(4,121)</span>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script src="script.js"></script>
</body>
</html>
$(document).ready(function(){
//Function usage
$(".side-bar .item-header a").on("click", function () {
addRemoveClass($(this),'.myarrow', 'ion-ios-arrow-down', 'ion-ios-arrow-up');
});
//Function definition
/**
* To add and remove class on a specific element
* @param {selector} clickItem where event will call (Example: .side-bar .item-header a)
* @param {class class} targetClass where icon will add and remove (Example: .myarrow)
* @param {class name} firstClass class name without (.)symbol (Example: ion-ios-arrow-down)
* @param {class name} secondClass class name without (.) symbol (Example: ion-ios-arrow-up)
*/
function addRemoveClass(clickItem, targetClass, firstClass, secondClass){
if ($(clickItem).find(targetClass).hasClass(firstClass)) {
$(clickItem).find(targetClass).removeClass(firstClass);
$(clickItem).find(targetClass).addClass(secondClass);
}else if($(clickItem).find(targetClass).hasClass(secondClass)){
$(clickItem).find(targetClass).removeClass(secondClass);
$(clickItem).find(targetClass).addClass(firstClass);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment