Displaying only icons in a Bootstrap 4 dropdown menu using jQuery

I am looking to design a unique Web Icon Font Dropdown Menu where the associated text is displayed next to the Flag Icons only when the menu is open.

Once a selection is made and the menu is closed, I want to show only the Active Flag Icon without any accompanying text.

Upon loading the webpage, one of the flags should already be active by default, such as Greece.

https://i.sstatic.net/97sJ4.jpg https://i.sstatic.net/qupzF.jpg

The following JQuery code is what I currently have, with a potential issue indicated by "this(TEXT)". I believe there might be room for improvement or alternative solutions using Bootstrap 4, possibly utilizing the active state to add or remove classes with jQuery.

$(".dropdown-menu li a").click(function(){
  $(this).parents(".dropdown").find('.btn').html($(this).text() + ' <span class="caret"></span>' + ' <span class="caret"></span>');
  $(this).parents(".dropdown").find('.btn').val($(this).data('value'));
});

This is my HTML Markup (Using Bootstrap 4)

<nav class="p-0 navbar navbar-expand fixed-top bg-white fixed-top">
    <div class="container">
        <ul class="container-fluid navbar-nav js-signin-modal-trigger justify-content-end list-unstyled" id="navbar2SupportedContent">
            <li class="dropdown">
                <a class="bg-light btn btn-default dropdown-toggle mx-0" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"><span class="flag-icon flag-icon-gr"></span></a>
                <ul class="dropdown-menu text-left" aria-labelledby="dropdownMenu1">
                    <li><a href="#" data-value="action"><span class="ml-2 label label-default"><span class="flag-icon flag-icon-gr"></span>Greece</span> </a> </li>
                    <li><a href="#" data-value="another action"><span class="ml-2 flag-icon flag-icon-fr"></span> <span>France</span></a></li>
                    <li><a href="#" data-value="something else here"><span class="ml-2 flag-icon flag-icon-us"></span> <span>United States</span></a></li>
                    <li><a href="#" data-value="separated link"><span class="ml-2 flag-icon flag-icon-es"></span><span> España</span></a></li>
                </ul>
            </li>
            <li><a href="#" data-signin="login" class="Loginbtn font-weight-bold btn navbar-btn px-md-4 px-sm-2 px-xs-1">Log in</a></li>                
            <li><a href="#" data-signin="signup" class="signupbtn text-white font-weight-bold btn navbar-btn px-md-4 px-sm-2 px-xs-1 ml-md-2 ml-sm-1 btn-secondary">Sign up</a></li>
        </ul>
    </div>
</nav>

If you would like to experiment with the code, feel free to check out this Fiddle: https://jsfiddle.net/stvu5ckj/1/

Inspired by the website www.nextdoor.com, my goal is to replicate a similar functionality that seamlessly transitions between the Flag Icons and corresponding text upon user interaction.

https://i.sstatic.net/13xqn.jpg

Answer №1

One recommendation would be to incorporate a JSON file containing ISO codes for all countries, then compare the code with Bootstrap's code in each match to retrieve the country name. You can find the JSON file here.

I hope this solution proves beneficial.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to use JQuery to open a new window on the same page when a link is clicked? It seems possible based

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Show Content with Lin ...

Unleashing the Power of Reactstrap: Making Uncontrolled Collapse Panels Open by Default

Can the uncontrolled collapse be displayed as open by default? Below is the standard code for uncontrolled collapse, which is initially shown as a collapsed tab. <Button color="primary" id="toggler"> Toggle </Button> < ...

What is the best way to incorporate a solid element onto a see-through background?

I have a link at "http://hastebin.com/julayefoto.xml" and I need assistance in making the content inside the bubble named "Test Test Test" non-transparent while keeping the bubble itself transparent. Any help would be greatly appreciated. Thank you :) ...

Utilizing JSON data to populate a modal window in HTML

How can I incorporate JSON data into an HTML modal window? I have a set of 12 buttons, and when a user clicks on one, I want to display the corresponding month's information from a JSON file. let myJson; $(`button`).on(`click`, function() { let ...

Implement a loading animation that will be displayed until a specific JavaScript code has finished loading

I need to implement a loading animation for an external JavaScript src while it's being loaded: <script src="https://xxxx.domain.com/xxx.js" type="text/javascript"></script> Currently, I am using jQuery (window).load, but it waits until ...

Can you please point me in the direction of where I can find the code that allows me to search and

I'm looking to customize the appearance of the "Show entries" and "Search" elements in a DataTable using Bootstrap for mobile devices. Upon inspecting the Developer Tools, I came across this code snippet: <div class="dataTables_length&quo ...

How can I close a dialog within an iframe in jQuery?

Suppose I initiate a dialog in this way: $('<iframe id="externalSite" class="externalSite" src="http://www.example.com" />').dialog({ autoOpen: true, width: 800, height: 500, modal: true, resizable: ...

What is the limit of CSS includes that IE can process?

While working on theming Drupal, I encountered an unusual issue. After enabling a few modules that added 5 to 10 link tags to the page, I noticed a strange behavior in different browsers. Firefox successfully added the new stylesheets to the cascade, but i ...

What steps can be taken to enhance the responsiveness of this Bootstrap 4 code?

I've been trying to make this code more responsive, but I haven't found a suitable solution yet. When the browser window shrinks to mobile sizes, the list becomes too narrow and the picture/video items on the right end up small and aesthetically ...

What is the best way to include a targeted href link within HTML code?

During the process of creating a website using bootstrap 4, I included the following snippet of HTML code: <form action="https://www.youtube.com/watch?v=pQN-pnXPaVg" target="_blank"> <button>Start</button> </form> Despite li ...

Is there a way to apply a duotone effect to any portion of a background image that is covered by an SVG?

I am looking to create an animated duotone effect around a background image using SVG filters. The goal is to have parts of the image covered by a moving SVG affected by the duotone effect. I want to achieve this effect dynamically without manually editing ...

Displaying a Bootstrap card with ellipsis text-overflow following a short excerpt of text

When working on a web application using the Bootstrap library, I utilized card classes. The goal was to truncate text that was too long, as shown in the image below: https://i.sstatic.net/4wKyT.jpg However, the result I achieved looked different than exp ...

Running JavaScript in Laravel without explicitly calling it

I am facing a strange issue with my code. I have an input type="button" element along with a JavaScript function in the same page. The goal is to update a table column only when the user presses the button. However, the JavaScript function gets executed ev ...

What is the best way to limit the dimensions of a responsive Google ad?

I have been using this responsive Google ad code on my website to display ads. <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display ...

Topmenu navigation with Bootstrap 4 navbar

I am currently working with a client who has a 140x140 logo that cannot be adjusted in size. To prevent excessive white space in the header, I am looking to implement two separate navbars: Topmenu, featuring a link to the login page or displaying a &apos ...

Press the button that says "start" using Javascript

Is it possible to modify the button click in this way? $('input[name="submit.one-click-premium-de.x"]').click().first(); Can I use a wildcard for the button name instead of specifying all words? $('input[name="submit.one-click*"]').c ...

Is it beneficial to switch from Prototype to jQuery in AJAX requests?

Here is a Javascript function I have written using Prototype: <script type="text/javascript> function go(qry) { new Ajax.Request('script.py?q=' + qry, {method: 'GET'} ); } </script> I am looking to conv ...

What is the most strategic way to conceal this overlay element?

Currently, the website I'm developing features a series of navigation elements at the top such as "Products" and "Company." Upon hovering over the Products link, an overlay displays a list of products with clickable links. Positioned at the top of the ...

The positioning of the input element within the div is set to

Currently, I have a container div for textbox (div.tb) with position:relative. Inside this div, there are input and placeholder divs, both of which have position:absolute. The issue lies in the fact that while the input text is vertically centered, the pl ...

Tips for arranging Bootstrap cards in ReactJS so they wrap to a new line after three have been displayed

In my personal portfolio, there is a dedicated page for showcasing various projects using Bootstrap cards. These projects are fetched from a CardData.js file and then mapped to a ProjectCard component. Ideally, I want to display 3 cards per row with consis ...