Reputation: 9
$(document).ready(function(){
if ($(window).width < '700')
{
$(".fevent").removeAttr('data-toggle data-target aria-expanded aria-controls');
$(".fevent").attr({"data-toggle":"collapse", "data-target":"#bol", "aria-expanded":"false", "aria-controls":"bol"});
$(".fevent").on("click",function(){
$("#bol").collapse('toggle');
});
} else if ($(window).width > '700')
{
$(".fevent").removeAttr('data-toggle data-target aria-expanded aria-controls');
$(".fevent").attr({"data-toggle":"collapse", "data-target":"#col", "aria-expanded":"false", "aria-controls":"col"});
$(".fevent").on("click", function(){
$("#col").collapse('toggle');
});
}
});
.fvent is the class of button , when i resize my window then the button should function differently, when the window is less than the 700 then clicking on button show div id #bol otherwise #col .
The button is working for width > 700 when i resizes the window less than 700 it still works as the width > 700.
PLEASE HELP ME OUT!!!
Upvotes: 0
Views: 110
Reputation: 1271
The $(document).ready()
is trigged only once so you are checking viewport width only on document load.
You have to add $(window).on('resize')
function and check viewport the size there.
Upvotes: 1
Reputation: 13666
You do not need to use any additional JavaScript for this. Just use 2 buttons and show and hide them with media queries:
.btn.col {
display:none;
}
@media (min-width: 700px) {
.btn.col {
display:inline-block;
}
.btn.bol {
display:none;
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<button class="btn btn-primary bol" type="button" data-toggle="collapse" data-target="#bol" aria-expanded="false" aria-controls="bol">Toggle bol</button>
<button class="btn btn-primary col" type="button" data-toggle="collapse" data-target="#col" aria-expanded="false" aria-controls="col">Toggle col</button>
<div class="row">
<div class="col">
<div class="collapse" id="col">
<div class="card card-body">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec venenatis enim ornare, ullamcorper ipsum ut, bibendum mi. Quisque sit amet velit dignissim, tincidunt neque nec, consequat dolor.
</div>
</div>
</div>
</div>
<div class="row">
<div class="col">
<div class="collapse" id="bol">
<div class="card card-body">
Vivamus auctor commodo nisl ut vestibulum. Aliquam erat volutpat. Aliquam eu leo non nunc ullamcorper fermentum. Donec vehicula dolor sed augue maximus, non congue urna semper.
</div>
</div>
</div>
</div>
Upvotes: 5
Reputation: 214
$(document).ready()
is triggered only the page loads; it's not triggered when the page is resized.
You can try $(window).on('resize',function(){ ...
Upvotes: 1