I have this accordion in my footer that should only run on mobile.
When I have it run on desktop initially the function doesn’t run, which is what I want. But if I switch to mobile view and then back to desktop, the function is still working… I’m still able to click the h5 and show/hide the text below… this shouldn’t be happening on desktop though, I don’t understand the issue..
Codepen so you can see the resize issue.
// On resize run the check screen size function
$(window).on("resize", function (e) {
checkScreenSize();
});
// Run the check screen size function on load
checkScreenSize();
// check screen size function
function checkScreenSize(){
var newWindowWidth = $(window).width();
// Run the accordion function on screens less than 1024
if (newWindowWidth < 1024) {
footerAccordion();
}
}
// Accordion function
function footerAccordion() {
$('.footer__locations p').hide();
$(".footer__locations").find("h5").click(function() {
$(".footer__locations").find("h5").removeClass('active');
$('.footer__locations p').slideUp();
var selected = $(this).next('.footer__locations p');
if (selected.is(":hidden")) {
$(this).next('.footer__locations p').slideDown();
$(this).toggleClass('active');
}
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<footer class="footer">
<div class="container-fluid">
<div class="row">
<div class="col-lg-6">
<div class="row footer__locations-row">
<div class="col-lg-6 footer__locations">
<h5>TITLE TWO</h5>
<p>THIS IS SOME TEXT TWO</p>
</div>
<div class="col-lg-6 footer__locations">
<h5>TITLE TWO</h5>
<p>THIS IS SOME TEXT TWO</p>
</div>
</div>
</div>
</div>
</div>
</footer>