How to modify arrows in bootstrap accordion? - javascript

I wrote some JavaScript and tried to replace the class whenever the aria-expanded element is true or false but nothing seems to be happening
The first card is open when the page loads because the aria-expanded ="true".
I want to find a way to replace the class in whenever the value of the aria-expanded element is changed. I'm new to jQuery and webdev so all of your help is appreciated.
Edit-I used bootstrap accordion to make sure only one can be open at a time
<div id="headingOne">
<h5 class="card-title">
Or Files
<a data-toggle="collapse" class="float-right" href="#collapseFiles"
role="button" aria-expanded="true" data-target="#collapseFiles"
aria-controls="collapseNotes">
<i class="fas fa-angle-up></i>
</a>
</h5>
</div>
<div id="headingTwo">
<h5 class="card-title">
Or Notes
<a data-toggle="collapse" class="float-right" href="#collapseNotes" role="button" aria-expanded="false" data-target="#collapseNotes" aria-controls="collapseNotes">
<i class="fas fa-angle-down"></i>
</a>
</h5>
</div>
<div id="headingThree">
<h5 class="card-title">
Or Names
<a data-toggle="collapse" class="float-right" href="#collapseNames" role="button" aria-expanded="false" data-target="#collapseNames" aria-controls="collapseNotes">
<i class="fas fa-angle-down"></i>
</a>
</h5>
</div>
and this is the jquery.
if ($('a').attr('aria-expanded').val() == 'true') {
$("a i").attr("class", "fa-angle-down");
} else {
$("a i").attr("class", "fa-angle-up");
}

I know you might want to do it using JavaScript, but I have a pure CSS way to do it:
HTML
I removed all unnecessary attributes on the anchor tag. I also removed fa-angle-down on the icon. The icon state will be set via CSS.
<div id="headingOne">
<h5 class="card-title">
Or Files
<a data-toggle="collapse" class="float-right" data-target="#collapseFiles">
<i class="fas"></i>
</a>
</h5>
<div class="card-body">
<p id="collapseFiles" class="card-text collapse show">
Some quick example text to build on the card title and make up the bulk of
the card's content.
</p>
</div>
</div>
<div id="headingTwo">
<h5 class="card-title">
Or Notes
<a data-toggle="collapse" class="float-right" data-target="#collapseNotes">
<i class="fas"></i>
</a>
</h5>
<div class="card-body">
<p id="collapseNotes" class="card-text collapse show">
Some quick example text to build on the card title and make up the bulk of
the card's content.
</p>
</div>
</div>
<div id="headingThree">
<h5 class="card-title">
Or Name
<a data-toggle="collapse" class="float-right" data-target="#collapseNames">
<i class="fas"></i>
</a>
</h5>
<div class="card-body">
<p id="collapseNames" class="card-text collapse show">
Some quick example text to build on the card title and make up the bulk of
the card's content.
</p>
</div>
</div>
CSS
You can write CSS to target any anchor tag that has data-toggle="collapse" attribute. When it collapses, Bootstraps adds .collapsed class so that you can use it to style the icon accordingly.
a[data-toggle="collapse"] i.fas:before {
content: "\f107"; /* angle-down */
}
a[data-toggle="collapse"].collapsed i.fas:before {
content: "\f106"; /* angle-up */
}
Screenshots
Fiddle Demo
https://jsfiddle.net/davidliang2008/og1t8ksj/26/

You need to use events to catch a change, for example
https://api.jquery.com/on/

Try this:-
$('[data-toggle="collapse"]').on('click', function(){
if ($(this).attr('aria-expanded') == 'true') {
$(this).find('i').addClass('fa-angle-up').removeClass('fa-angle-down');
} else {
$(this).find('i').addClass('fa-angle-down').removeClass('fa-angle-up');
}
});
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel='stylesheet'>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" ></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js#1.16.0/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
<div id="headingTwo">
<h5 class="card-title">
Or Notes
<a data-toggle="collapse" class="float-right" href="#collapseNotes" role="button" aria-expanded="false" data-target="#collapseNotes" aria-controls="collapseNotes">
<i class="fa fa-angle-up"></i>
</a>
</h5>
</div>
<div class="collapse" id="collapseNotes">
Anim pariatur cliche reprehenderit
</div>

It's been a while, but I think this is probably a good answer to this question:
NO NEED TO USE JAVASCRIPT for this. Take advantage of the class 'collapsed' which is added to the wrapping 'a' when the accordion is collapsed.
<a data-toggle="collapse" class="float-right" href="#collapseFiles"
role="button" aria-expanded="true" data-target="#collapseFiles"
aria-controls="collapseNotes">
<span class="arrowup"> <i class="fas fa-angle-up></i> </span>
<span class="arrowdown"> <i class="fas fa-angle-dowm></i> </span>
</a>
then on your stylesheet:
.accordion .arrowdown {
display: none
}
----------
.accordion .collapsed .arrowup {
display: none
}
---------
.accordion .collapsed .arrowdown{
display: block
}

Related

Collapsible sidebar with Bootstrap 5 was working before but not now

I have this sidebar
which should be toggled when the button on the right corner of the photo is clicked. Unfortunately it is not working now but it was worked before. I have no idea what might be the problem.
Here is the HTML output for the sidebar
<nav id="sidebar">
<div class="bg-dark h-100" id="sidebar-wrapper">
<div class="sidebar-heading bg-dark text-light fw-bold newFont2">
<i class="fa-solid fa-globe me-1"></i> domain.com
</div>
<div class="list-group list-group-flush">
<a class="list-group-item bg-sidebar px-3 py-2" href="https://example.com/company/dashboard" current-page="absolute">
<i class=" fa-solid fa-house-user text-info "></i>
<span class="align-middle">Home</span>
</a>
<a class="list-group-item bg-sidebar px-3 py-2" href="https://example.com/company/dashboard/reviews">
<i class=" fa-solid fa-star-half-stroke "></i>
<span class="align-middle">Reviews</span>
</a>
<a class="list-group-item bg-sidebar px-3 py-2" href="https://example.com/company/dashboard/invitations">
<i class=" fa-solid fa-envelope "></i>
<span class="align-middle">Invitations</span>
</a>
<a class="list-group-item bg-sidebar px-3 py-2" href="https://example.com/company/dashboard/settings">
<i class=" fa-solid fa-wrench "></i>
<span class="align-middle">Settings</span>
</a>
<div class="border-top my-3" href="#"></div>
<div class="plan-overview px-3">
<span class="text-white-50 newFont d-block">Selected plan: <span class="fw-bold text-light">Expert Plan</span>
<br>Available actions: <span class="fw-bold text-light ">646 left</span>
<br>
</span>
</div>
</div>
</div>
</nav>
And here's the button
<button class="btn navbar-toggler2" id="sidebarToggle"><i class="navbar-toggler-icon"></i></button>
I'm using Bootstrap v5.0.2. I can actually see following the scripts that are loaded on the page, that I'm using something called Simple Sidebar
/*!
* Start Bootstrap - Simple Sidebar v6.0.3 (https://startbootstrap.com/template/simple-sidebar)
* Copyright 2013-2021 Start Bootstrap
* Licensed under MIT (https://github.com/StartBootstrap/startbootstrap-simple-sidebar/blob/master/LICENSE)
*/
window.addEventListener("DOMContentLoaded",(e=>{const t=document.body.querySelector("#sidebarToggle");t&&t.addEventListener("click",(e=>{e.preventDefault(),document.body.classList.toggle("sb-sidenav-toggled"),localStorage.setItem("sb|sidebar-toggle",document.body.classList.contains("sb-sidenav-toggled"))}))}));
First check if on click of the button, does body toggles class sb-sidenav-toggled, if it does, check your css as it works on css, the #sidebar-wrapper should get margin-left after the class toggles.
Otherwise it's js problem, not loaded/somethings stops it before it get's triggered, or some errors within ?
EDIT:
So it was just something preventing or not declared properly onclick, so declaring on click was solution:
$("#sidebarToggle").on('click', function() {$('body').toggleClass('sb-sidenav-toggled');});

align element in footer to right

I have footer,where i have copyright mark with name and also instagram link, which i want align to the right. Nothing work, can you help me please? Im using bootstrap.
<footer class="text-center bg-dark py-3" style="color: white;">
<span>name &nbsp</span><span style="color:#d64161">©2021 &nbsp</span><i style="color: white;" class="fab fa-instagram"> instagram</i>
</footer>
change text-center to text-right
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css" integrity="sha512-P5MgMn1jBN01asBgU0z60Qk4QxiXo86+wlFahKrsQf37c9cro517WzVSPPV1tDKzhku2iJ2FVgL67wG03SGnNA==" crossorigin="anonymous" />
<footer class="text-right bg-dark py-3" style="color: white;">
<span>name &nbsp</span><span style="color:#d64161">©2021 &nbsp</span>
<a href="https://www.instagram.com/dtd_quiz/" target="_blank">
<i style="color: white;" class="fab fa-instagram"> instagram</i>
</a>
</footer>
There are 2 ways to do it.
Use text-right class on footer element,
on the first span give these 2 classes: d-inline-block and ml-auto and d-flex to footer;
I would suggest the first one.
please let me know if this won't help.
Add this CSS
footer.text-center.bg-dark.py-3 {
text-align: right !important;
}

Plus and Minus icons on bootstrap accordion

I'm trying to toggle between plus and minus icon for bootstrap accordion. On default, plus icon is showing. When accordion collapses, it shows the minus icons and hides the plus icon. I don't know if what I'm saying makes sense but it's really basic for some of you. I'm just a beginner.
I've tried doing it with css with :after and content "+" and content "-" but this is not what I want. I need to use font awesome. I've also tried the js way.
<script>
$(document).ready(function(){
// Toggle plus minus icon on show hide of collapse element
$(".collapse").on('show.bs.collapse', function(){
$(this).prev(".card-header").find(".fa").removeClass("fa-plus").addClass("fa-minus");
}).on('hide.bs.collapse', function(){
$(this).prev(".card-header").find(".fa").removeClass("fa-minus").addClass("fa-plus");
});
});
</script>
<body>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Main content -->
<section class="content container-fluid">
<legend><?php echo $header_title; ?></legend>
<br>
<div class="row">
<div class="col-md-12 accordion-container">
<div class="accordion" id="accordionExample">
<div class="card">
<div class="card-header" id="headingTwo">
<h2 class="mb-0" style="margin: 10px !important">
<button id="btnCollapse" class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
<i class="fa fa-plus"></i>
Card Info
</button>
</h2>
</div>
<div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionExample">
<div class="card-body">
<!-- SOME CONTENT -->
</div>
</div>
</div>
</div>
</div>
</div>
</section>
I don't know how to check where I went wrong. All I want is to show the plus and minus icon
$(document).ready(function() {
// Toggle plus minus icon on show hide of collapse element
$(".collapse").on('show.bs.collapse', function() {
$(this).prev(".card-header").find(".fa").removeClass("fa-plus").addClass("fa-minus");
}).on('hide.bs.collapse', function() {
$(this).prev(".card-header").find(".fa").removeClass("fa-minus").addClass("fa-plus");
});
});
/*$("#btnCollapse").on("click", function() {
$(".fa-plus").toggleClass("fa-minus");
})
*/
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<body>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Main content -->
<section class="content container-fluid">
<legend>
<?php echo $header_title; ?>
</legend>
<br>
<div class="row">
<div class="col-md-12 accordion-container">
<div class="accordion" id="accordionExample">
<div class="card">
<div class="card-header" id="headingTwo">
<h2 class="mb-0" style="margin: 10px !important">
<button id="btnCollapse" class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
Card Info
<i class="fa fa-plus"></i>
</button>
</h2>
</div>
<div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionExample">
<div class="card-body">
<!-- SOME CONTENT -->
</div>
</div>
</div>
</div>
</div>
</div>
</section>
You can use alternate solution as follow:
$(document).ready(function(){
// Add minus icon for collapse element which is open by default
$(".collapse.show").each(function(){
$(this).prev(".card-header").find(".fa").addClass("fa-minus").removeClass("fa-plus");
});
// Toggle plus minus icon on show hide of collapse element
$(".collapse").on('show.bs.collapse', function(){
$(this).prev(".card-header").find(".fa").removeClass("fa-plus").addClass("fa-minus");
}).on('hide.bs.collapse', function(){
$(this).prev(".card-header").find(".fa").removeClass("fa-minus").addClass("fa-plus");
});
});
Alex, your code is working fine. Please check this fiddle.
[https://jsfiddle.net/boilerplate/bootstrap][1]
Please make sure your js code must be written after the jquery.js and bootstrap.js loaded.

JavaScript- Not showing the Title in Text Accordion

At first Please see the screenshot , here i am working on Accordion text Editor, where you can add title and body text.
when i clicked ADD button multiple time then showing like this type. Not showing the title text. i thing it's problem on JS file. Does any solution??
Thanks in Advance!
$(document).ready(function() {
$('#Add_btn').click(function(){
$('#accordion_body').append($('#editableDiv').html());
var x = document.getElementById("accordion_input").value;
document.getElementById("accordion_title").innerHTML = x;
});
});
function myAccorFunction() {
var Myaccordion = `
<div class="Myacc" id="acc_main" contenteditable="false"
class="accordion-main"
style="position: relative;width: 98.5%; left: 9px;">
</div>
<div class="panel-group trash_removed" id="accordion" role="tablist"
aria-multiselectable="true"
style="margin-bottom: 7px;margin-top: 10px"
contenteditable="false">
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingOne"
contenteditable="false">
<h4 class="panel-title">
<a id="accordion_title" role="button" data-toggle="collapse"
data-parent="#accordion" href="#expandingCol"
aria-expanded="false" aria-controls="collapseOne">
</a><div class="icon_area">
<i class="fa fa-edit" data-toggle="collapse"
data-parent="#accordion" href="#collapseOne"
aria-expanded="false" aria-controls="collapseOne">
</i>
<i onclick="myfundelete()" class="fa fa-trash">
</i>
</div>
</h4>
</div>
<div id="expandingCol" class="panel-collapse collapse"
role="tabpanel" aria-labelledby="headingOne">
<div id="accordion_body" class="panel-body" >
</div>
</div>
</div>
</div>
`;
$("#editor").append(Myaccordion);
}
<button onclick="myAccorFunction()" id="Add_btn"
class="btn btn-primary note-image-btn" role="button"
data-toggle="collapse" data-parent="#accordion"
href="#collapseOne" aria-expanded="true"
aria-controls="collapseOne">Add</button>
It appears you are using the same element ID #accordion_title more than once. As browsers assume element ID is unique within a page, document.getElementById("accordion_title").innerHTML = x; will only change the innerHTML of the first element having that ID, but not the subsequent ones that were appended by the ADD button.
Try using a Class name instead of ID for your title text element, then iterate over every element of that class to change their innerHTML.
$('.accordion_title').each(function() {
$(this).html($('#accordion_input').val());
});

How to collapse content and change glyphicon in buttons right in bootstrap?

In the lack of my javascript-skills I need some help for my very basic bootstrap problem.
That's the goal: four hidden paragraphs via collapse and a "toggle" button for each to make it visible or hidden.
That's the problem: the first collapse works fine, but the second, third and fourth glyphicon inside the buttons is changing too. And if I want to view the second, third and fourth collapsed paragraph after clicking the appropriate button it only shows the first paragraph. What happen? Is there only the one way to make this works, to include different IDs for each paragraph in the Javascript?
Live on Bootply: http://www.bootply.com/Jyf06v1aiK
<div class="col-xs-6">
<div class="collapse" id="collapseDiv">
<p class="">This is the FIRST collapsible content!</p>
</div>
<button data-toggle="collapse" data-target="#collapseDiv">
<span class="glyphicon glyphicon-large glyphicon-menu-down"></span>
</button>
</div>
<div class="col-xs-6">
<div class="collapse" id="collapseDiv">
<p class="">This is the SECOND collapsible content!</p>
</div>
<button data-toggle="collapse" data-target="#collapseDiv">
<span class="glyphicon glyphicon-large glyphicon-menu-down"></span>
</button>
</div>
<div class="col-xs-6">
<div class="collapse" id="collapseDiv">
<p class="">This is the THIRD collapsible content!</p>
</div>
<button data-toggle="collapse" data-target="#collapseDiv">
<span class="glyphicon glyphicon-large glyphicon-menu-down"></span>
</button>
</div>
<div class="col-xs-6 bg-success">
<div class="collapse" id="collapseDiv">
<p class="">This is the FOURTH collapsible content!</p>
</div>
<button data-toggle="collapse" data-target="#collapseDiv">
<span class="glyphicon glyphicon-large glyphicon-menu-down"></span>
</button>
</div>
Javascript:
$('#collapseDiv').on('shown.bs.collapse', function () {
$(".glyphicon").removeClass("glyphicon-menu-down").addClass("glyphicon-menu-up");
});
$('#collapseDiv').on('hidden.bs.collapse', function () {
$(".glyphicon").removeClass("glyphicon-menu-up").addClass("glyphicon-menu-down");
});
Take a look at my changes on your code here.
Your issue is not actually related to JS, but to concepts around the DOM.
First of all, it isn't a good idea to have the same id in multiple elements, the id attribute is supposed to be unique across the entire DOM.
Second, $('.glyphicon') refers to ALL the elements in the DOM with a class glyphicon not only the closest to your collapse div.
I'm including the code here but feel free to try it in the link above.
<div class="container">
<h1 class="">Bootstrap Collapse Buttons (WIP)</h1>
<div class="col-xs-6 bg-warning">
<div class="collapse customCollapse" id="collapseDiv">
<p class="">This is the FIRST collapsible content!</p>
</div>
<button data-toggle="collapse" data-target="#collapseDiv" class=""> <span class="glyphicon glyphicon-large glyphicon-menu-down"></span>
</button>
</div>
<div class="col-xs-6 bg-info">
<div class="collapse customCollapse" id="collapseDiv2">
<p class="">This is the SECOND collapsible content!</p>
</div>
<button data-toggle="collapse" data-target="#collapseDiv2" class=""> <span class="glyphicon glyphicon-large glyphicon-menu-down"></span>
</button>
</div>
<div class="col-xs-6 bg-danger">
<div class="collapse customCollapse" id="collapseDiv3">
<p class="">This is the FIRST collapsible content!</p>
</div>
<button data-toggle="collapse" data-target="#collapseDiv3" class=""> <span class="glyphicon glyphicon-large glyphicon-menu-down"></span>
</button>
</div>
<div class="col-xs-6 bg-success">
<div class="collapse customCollapse" id="collapseDiv4">
<p class="">This is the SECOND collapsible content!</p>
</div>
<button data-toggle="collapse" data-target="#collapseDiv4" class=""> <span class="glyphicon glyphicon-large glyphicon-menu-down"></span>
</button>
</div>
</div>
And the js
$('.customCollapse').on('shown.bs.collapse', function () {
$(this).parent().find(".glyphicon").removeClass("glyphicon-menu-down").addClass("glyphicon-menu-up");
});
$('.customCollapse').on('hidden.bs.collapse', function () {
$(this).parent().find(".glyphicon").removeClass("glyphicon-menu-up").addClass("glyphicon-menu-down");
});
EDIT
A little extra explanation: if you plan to share js logic across multiple DOM elements a good way to do it is using classes. That is why I changed your js and DOM to use .customCollapse instead of the collapseDiv id.
Note also that I changed the ids on each collapseDiv by adding a number to make it unique.

Categories