Reputation: 1999
I have a situation in which I am using Bootstrap collapse to create growing and shrinking content.
I have a class called my-btn which has a background image of a downwards arrow. When I click on this button I want to add a class called open.
.my-btn{
background-image:url(../img/template-1/read-more-closed.png);
background-repeat:no-repeat;
background-position:center;
width:50px;
height:50px;
margin-bottom:50px;
}
.open{
background-image:url(../img/template-1/read-more-open.png);
background-repeat:no-repeat;
background-position:center;
width:50px;
height:50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<button class="btn my-btn" type="button" data-toggle="collapse" data-target="#collapse4"
aria-expanded="false" aria-controls="collapseExample"> </button>
By adding the open class after my-btn, the arrow is changed... but how can I acheive this using CSS/js?
UPDATE
I have tried
<script>
$(document).ready(function(){
$('.my-btn').on('click', function(){
$(this).toggleClass('open');
});
});
</script>
But on toggle, bootstrap adds the collaPSED CLASS BUT MY CODE IS IGNORED.
Upvotes: 1
Views: 331
Reputation: 518
You can quickly and easily achieve this using an on click function combined with a toggle class function for adding/removing classes after your button is clicked.
See below for working code.
$('.btn').on('click', function() {
$(this).toggleClass('open');
});
.my-btn{
background-image:url(../img/template-1/read-more-closed.png);
background-repeat:no-repeat;
background-position:center;
width:50px;
height:50px;
margin-bottom:50px;
}
.open{
background-image:url(../img/template-1/read-more-open.png);
background-repeat:no-repeat;
background-position:center;
width:50px;
height:50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<button class="btn my-btn" type="button" data-toggle="collapse" data-target="#collapse4"
aria-expanded="false" aria-controls="collapseExample"> </button>
Upvotes: 1
Reputation: 22474
You can do something like this:
.my-btn{
background-image:url(../img/template-1/read-more-closed.png);
background-repeat:no-repeat;
background-position:center;
width:50px;
height:50px;
margin-bottom:50px;
}
.open{
background-image:url(../img/template-1/read-more-open.png);
background-repeat:no-repeat;
background-position:center;
width:50px;
height:50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<button class="btn my-btn" type="button" data-toggle="collapse" data-target="#collapse4"
aria-expanded="false" aria-controls="collapseExample" onclick="this.classList.toggle('open')"> </button>
The important part is onclick="this.classList.toggle('open')"
, this JavaScript code will toggle the "open" class (remove it if it exists or add it if it doesn't).
Keep in mind that the classList.toggle
function is not supported by all browser. You may have to use some polyfill if you want to support all browsers.
Upvotes: 1
Reputation: 18123
You can use the toggleClass()
function:
$('.my-btn').on('click', function() {
$(this).toggleClass('open');
});
Upvotes: 0