I have a simple issue with my dropdown button while I'm trying to get the selected dropdown item to the dropdown button value.
The Flag Icon and Text should be changed dynamically I made some examples but as I see it's not working as I expected.
The only issue that I have is the flag icon not changed dynamically
Live Example: https://codepen.io/themes4all/pen/wvdemKz
HTML:
<div class="dropdown">
<button class="btn btn-primary btn-lg dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false"><span class="flag-icon flag-icon-us me-1"></span> <span>English</span></button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
<li>
<a class="dropdown-item active" href="#"><span class="flag-icon flag-icon-us me-1"></span> <span>English</span></a>
</li>
<li>
<a class="dropdown-item" href="#"><span class="flag-icon flag-icon-fr me-1"></span> <span>French</span></a>
</li>
<li>
<a class="dropdown-item" href="#"><span class="flag-icon flag-icon-es me-1"></span> <span>Spanich</span></a>
</li>
<li>
<a class="dropdown-item" href="#"><span class="flag-icon flag-icon-sa me-1"></span> <span>Arabic</span></a>
</li>
</ul>
</div>
jQuery
if ($(".dropdown").length) {
$(document).on("click", ".dropdown-menu .dropdown-item", function (e) {
e.preventDefault();
if (!$(this).hasClass("active")) {
var swalWithBootstrapButtons = Swal.mixin({
customClass: {
confirmButton: "btn btn-primary",
cancelButton: "btn btn-danger me-3",
},
buttonsStyling: false,
});
swalWithBootstrapButtons
.fire({
title: "Are you sure?",
text: "Do you really want to change your current language!",
icon: "warning",
confirmButtonText: "<i class='fas fa-check-circle me-1'></i> Yes, I am!",
cancelButtonText: "<i class='fas fa-times-circle me-1'></i> No, I'm Not",
showCancelButton: true,
reverseButtons: true,
focusConfirm: true,
})
.then((result) => {
if (result.isConfirmed) {
if (!$(this).hasClass("active")) {
$(".dropdown-menu .dropdown-item").removeClass("active");
$(this).addClass("active");
$(this)
.parents(".dropdown")
.find(".btn")
.html("<span class='flag-icon flag-icon-us me-1'></span>" + $(this).text());
}
Swal.fire({
icon: "success",
title: "Amazing!",
text: "Your current language has been changed successfully.",
showConfirmButton: false,
timer: 1500,
});
}
});
}
});
}
you need to replace .html("<span class='flag-icon flag-icon-us me-1'></span>" + $(this).text());
with .html($(this).html());
, it will give you the desired output
Please check this, I have just updated the code to get class of clicked option and added to the span in HTML
if ($(".dropdown").length) { $(document).on("click", ".dropdown-menu .dropdown-item", function (e) { e.preventDefault(); var cloneflagicon = $(this).find('span.flag-icon').attr('class'); if (!$(this).hasClass("active")) { var swalWithBootstrapButtons = Swal.mixin({ customClass: { confirmButton: "btn btn-primary", cancelButton: "btn btn-danger me-3", }, buttonsStyling: false, }); swalWithBootstrapButtons .fire({ title: "Are you sure?", text: "Do you really want to change your current language!", icon: "warning", confirmButtonText: "<i class='fas fa-check-circle me-1'></i> Yes, I am!", cancelButtonText: "<i class='fas fa-times-circle me-1'></i> No, I'm Not", showCancelButton: true, reverseButtons: true, focusConfirm: true, }) .then((result) => { if (result.isConfirmed) { if (!$(this).hasClass("active")) { $(".dropdown-menu .dropdown-item").removeClass("active"); $(this).addClass("active"); $(this) .parents(".dropdown") .find(".btn") .html("<span class='"+cloneflagicon+"'></span>" + $(this).text()); } Swal.fire({ icon: "success", title: "Amazing!", text: "Your current language has been changed successfully.", showConfirmButton: false, timer: 1500, }); } }); } }); }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Bootstrap 5</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" /> <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.4.0/font/bootstrap-icons.css" rel="stylesheet" /> <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" rel="stylesheet" /> <link href="https://cdnjs.cloudflare.com/ajax/libs/flag-icon-css/3.5.0/css/flag-icon.min.css" rel="stylesheet" /> </head> <body> <div class="dropdown"> <button class="btn btn-primary btn-lg dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false"><span class="flag-icon flag-icon-us me-1"></span> <span>English</span></button> <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1"> <li> <a class="dropdown-item active" href="#"><span class="flag-icon flag-icon-us me-1"></span> <span>English</span></a> </li> <li> <a class="dropdown-item" href="#"><span class="flag-icon flag-icon-fr me-1"></span> <span>French</span></a> </li> <li> <a class="dropdown-item" href="#"><span class="flag-icon flag-icon-es me-1"></span> <span>Spanich</span></a> </li> <li> <a class="dropdown-item" href="#"><span class="flag-icon flag-icon-sa me-1"></span> <span>Arabic</span></a> </li> </ul> </div> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script> </body> </html>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.