I will be creating left vertical (sidebar) navigation menu by referring "Example" in https://getbootstrap.com/docs/5.0/components/accordion/.
Suppose we have 3 buttons in HTML as follows'
<button onclick="myFunction1()">Button 1</button>
<button onclick="myFunction2()">Button 2</button>
<button onclick="myFunction3()">Button 3</button>
I want to implement functionality like if user click on Button 1 then only "Accordion Item #1" should expand. Similarly if user click on Button 2 then only "Accordion Item #2" should expand and if ser click on Button 3 then only "Accordion Item #3" should expand.
I want to know which are classes in bootstrap I need to uses to expand/collapse a particular menu item dyamically so that I can use them in jQuery. Could someone please provide any code or let me know atleast classes in bootstrap I need to uses to expand/collapse a particular menu item dyamically?
CodePudding user response:
How it works (in the last version of bootstrap)
Edit: i read more in bootstrap docs.. collapse not is a component of the accordions .. (i dont use bootstrap)
Collapse have a own documentation that explain more how to trigger collapsing events .
But have the same logic at my answer (Just copy and paste the button) and works so .. i'll keep the same !
Just learn more how to trigger all that better and more specific than copy and paste the button . (I dont use bootstrap)
Better overall than creating some 'external' code to do this!
Just copy and paste the button !!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flexbox</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,300;0,400;0,500;1,300;1,400;1,500&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-cuYeSxntonz0PPNlHhBs68uyIAVpIIOZZ5JqeqvYYIcEL727kskC66kF92t6Xl2V" crossorigin="anonymous"></script>
<style>
@import url('https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,300;0,400;0,500;1,300;1,400;1,500&display=swap');
* {
margin: 0;
padding: 0;
box-sizing: border-box;
text-decoration: none;
list-style: none;
outline: none;
font-family: 'Poppins', 'system-ui', 'sans-serif';
font-weight: 300;
transition: .3s linear 0s all;
}
h6 {
font-weight: 600;
}
h5, h4 {
font-weight: 500;
}
main {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
flex-wrap: nowrap;
gap: 20px;
padding-top: 20px;
}
main > section {
display: inline-flex;
flex-direction: row;
flex-wrap: nowrap;
gap: 20px;
}
main > section:nth-child(1) {
background: #f3f57e;
}
main > section > div {
flex: 0 0 50%;
}
</style>
</head>
<body>
<main>
<section>
<div>
<div id="accordionExample">
<div >
<h2 id="headingOne">
<button type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
Accordion Item #1
</button>
</h2>
<div id="collapseOne" aria-labelledby="headingOne" data-bs-parent="#accordionExample">
<div >
<strong>This is the first item's accordion body.</strong> It is shown by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
</div>
</div>
</div>
<div >
<h2 id="headingTwo">
<button type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
Accordion Item #2
</button>
</h2>
<div id="collapseTwo" aria-labelledby="headingTwo" data-bs-parent="#accordionExample">
<div >
<strong>This is the second item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
</div>
</div>
</div>
<div >
<h2 id="headingThree">
<button type="button" data-bs-toggle="collapse" data-bs-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
Accordion Item #3
</button>
</h2>
<div id="collapseThree" aria-labelledby="headingThree" data-bs-parent="#accordionExample">
<div >
<strong>This is the third item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
</div>
</div>
</div>
</div>
</div>
<div>
<button type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
Accordion Item #1
</button>
<button type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
Accordion Item #2
</button>
<button type="button" data-bs-toggle="collapse" data-bs-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
Accordion Item #3
</button>
</div>
</section>
</main>
</body>
<script>
</script>
</html>