How to use Jquery how to change the aria-expanded="false" part of a dom element (Bootstrap)?
You can use .attr() as a part of however you plan to toggle it:
$("button").attr("aria-expanded","true");
Since the question asked for either jQuery or vanilla JS, here's an answer with vanilla JS.
I've added some CSS to the demo below to change the button's font color to red when its aria-expanded
is set to true
const button = document.querySelector('button');button.addEventListener('click', () => { button.ariaExpanded = !JSON.parse(button.ariaExpanded);})
button[aria-expanded="true"] { color: red;}
<button type="button" aria-expanded="false">Click me!</button>
Bootstrap Toggle class based on an element's click. aria-expanded value is mentioned after a click
$("#navbar-btn-icon").click(function(e) { var menuItem = $(this); if (menuItem.attr("aria-expanded") === "true") { $(".navbar-toggler-icon").addClass('clicked'); } else if (menuItem.attr("aria-expanded") === "false") { $(".navbar-toggler-icon").removeClass('clicked'); }});
Add close button icon in CSS. Changes on click
.navbar-toggler-icon.clicked{ background-image: url("close-icon.png"); transition: 0.2s ease-in;}