I have two div elements in my application. I want to randomly show and hide these divs when a navigation link is clicked and the next page loads completely.
The example below does not include multiple pages, only a single page. Therefore, I want to randomly show and hide the divs across multiple page loads.
For example, the first div is always visible, so if a user clicks on any navigation link and the next page opens, then the first div hides and the second div shows. The same applies for other navigation link clicks - switch between the two divs randomly.
Code :
$(".navbar .nav-link").click(function() {
if ($('.card1:visible').length) {
$('.card1').hide();
$('.card2').show();
} else {
$('.card1').show();
}
});
.hide {
display: none;
}
.Wrap .card img {
height: 250px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a9cbc6c6dd*******bc8d9e99c8798879a">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
<li class="nav-item">
<a class="nav-link disabled">Disabled</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="Wrap">
<div class="card card1 " style="width: 18rem;">
<img src="https://picsum.photos/200/300" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">Home Content</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<a href="#" class="btn btn-primary">Go somewhere</a>
</div>
</div>
<div class="card card2 hide" style="width: 18rem;">
<img src="https://picsum.photos//id/237/200/300" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">About Content</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<a href="#" class="btn btn-primary">Go somewhere</a>
</div>
</div>
</div>