How can you dynamically add 'subtext' to a selectpicker in Bootstrap 4?

While working with Bootstrap-4, I am attempting to dynamically add 'data-subtext' next to all the option texts of a selectpicker. The code below successfully creates the list of options from an array, but I am struggling to include the data-subtext text as well.

    <!---HTML section, utilizing function to generate list of options  ------>      
                 <div class="form-group mr-2">        
                    <div class="col-mr-2">
                        <div class="input-group">
                            <div class="input-group-append">
                        <select class="selectpicker w-100"  data-show-subtext="true"  data-live-search="true" id="buttNameSel" name="buttNameF" onChange="replacButtName()">                
                              <option data-subtext="mysubtext_list" >select-from:</option>                          
   <!------   autofill the options---------->   
                      </select>
                            </div>
                        </div>
                    </div>
                 </div>
                <script type="text/javascript" > 
                // set option list of names
                selButtname = document.getElementById( 'buttNameSel' );
                var opt;
                for (i=0; i < nItems;i++) {
                   buttName = ButtNamesSplitList[i];
                   opt = document.createElement("option"); 
                   opt.text=buttName ;
                   selButtname.add(opt);    
                    }
                    $('.selectpicker').selectpicker('refresh');
                </script> 

Answer №1

To enhance your code, simply update it by including the attribute in this manner:

  <script type="text/javascript" > 
                // establish option list of names
                selButtname = document.getElementById( 'buttNameSel' );
                var opt;
                for (i=0; i < nItems;i++) {
                   buttName = ButtNamesSplitList[i];
                   opt = document.createElement("option"); 
                   opt.text=buttName ;
                   opt.setAttribute("data-subtext", "your_sub_text"); //insert subtext here
                   selButtname.add(opt);    
                    }
                    $('.selectpicker').selectpicker('refresh');
 </script> 

If you want to learn more about how setAttribute functions, refer to this documentation: https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute

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

Steps for implementing a select all feature with jQuery

I'm currently working on a jQuery function that is acting as a select-all option. Everything is functioning correctly except that when I deselect any of the child elements from select all, the option remains enabled. My goal is to enable or disable th ...

What is the process for utilizing GruntFile.coffee and package.json to extract or create the Lungo.js example files?

I want to experiment with the Lungo.js examples found here: https://github.com/tapquo/Lungo.js. However, when I try to run the index.html in the example directory, it seems like the components and package directories are empty. Although these directories d ...

The AngularJs 2 framework encountered an issue with booting up after attempting to combine all TypeScript files into a single JavaScript file

I am currently utilizing Angular 2 with TypeScript (V-1.8) in my project setup. I have configured my tsconfig to output the code into a single .js file. This single.js file includes the necessary code to bootstrap the application, as the boot.ts file is al ...

Add a fading transition feature to images that are loaded at a later time

I used a clever technique to create a blur effect by loading a small, lightweight image first. Once the main background image is loaded, it swaps out the 'data-src' with the actual image. However, I am facing an issue with the abrupt transition, ...

Combining Meshes in three.js LOD

I am searching for an efficient way to utilize the LOD Object from three.js (view example here). My concept involves creating an LOD method similar to the one outlined in chapter 2.1 (found here). The structure consists of 3 levels: a 3D model in close ...

The function d3.json() does not support googleoverlay

As a newcomer to coding, I am currently working on incorporating the code found at https://bl.ocks.org/mbostock/899711. Instead of using a local .json file, I have opted to read JSON data from a URL. However, I encountered an issue where the LAT and LONG v ...

Emerald: Fresh alert for numerous attributes

After updating jade to the latest version, I started seeing a message in the console that says: You should not have jade tags with multiple attributes This change was mentioned as a feature here 0.33.0 / 2013-07-12 Hugely more powerful error reporting ...

Animating fjdxsu using Threejs formula

Can you provide me with the exact formula that should be used in the animate function? ...

AngularJS - Struggling to retrieve value from ng-model in controller

Having trouble using Angular's ng-model to store and pass values with ng-click? If you're receiving undefined values in your current code, here's a possible solution. Check out the HTML snippet below: HTML <ion-content> <div c ...

Access values in object array without iterating over it

I'm wondering if there is a way to extract the values of the name property from an object array without having to iterate through it. var objArray = [ { name: 'APPLE', type: 'FRUIT' }, { name: 'ONION', t ...

Utilizing the js-yaml library to parse a YAML document

Currently, I'm utilizing js-yaml to analyze and extract the data from a yaml file in node js. The yaml file consists of key-value pairs, with some keys having values formatted like this: key : {{ val1 }} {{ val2 }} However, the parsing process enco ...

Tips for handling imports and dependencies in a React component that is shared through npm

Hello everyone, I'm diving into the world of sharing React components and have encountered an interesting challenge that I hope you can help me with. Currently, I have a button component in my app that is responsible for changing the language. My app ...

Expanding the size of one div causes the surrounding divs to shift positions

I am currently working on creating a row of divs that expand when hovered over by the mouse. I have managed to achieve this functionality, but now I want the expanding div to cover its neighboring divs partially, without affecting their sizes or moving the ...

Issue with dropdown selection not functioning properly in Bootstrap 4.1.0 release

When I click on the select options, I want a dropdown with icons to appear. Requirement: The code needs to be updated for BootStrap version 4.1.0. Currently, clicking on the select dropdown does not display anything. Actual Output: This code works perfec ...

Express: simplifying the use of middleware for app implementation

I am looking to update the code in my index.js file app.use(require('sass-middleware').middleware({ src: path.resolve(__dirname, '../'), dest: path.resolve(__dirname, '../public') })); app.use(require('browserify-dev ...

An issue occurred while trying to serialize cookies retrieved in getServerSideProps

I am currently working on a project using Reactjs with Next.js. I recently implemented a login module using cookies, but encountered an issue when trying to serialize the .cookies object returned from getServerSideProps. The error message states that undef ...

How can I display a pre-existing div with a dropdown button?

I have individual div elements for each type of clothing item (shirt, pant, suit) that I want to display when the corresponding service is selected. This means that when I click on one of them, only that specific div will be shown. I am looking for a solut ...

The Vanilla JS script in Next.js fails to execute upon deployment

Currently developing a simple static site with Next.js. The lone vanilla JS script in use is for managing a mobile menu - enabling toggling and adding a specific class to disable scrolling on the body: if (process.browser) { document.addEventListener(&ap ...

The HTML checkbox remains unchanged even after the form is submitted

On a button click, I have a form that shows and hides when the close button is clicked. Inside the form, there is an HTML checkbox. When I check the checkbox, then close the form and reopen it by clicking the button again, the checkbox remains checked, whi ...

My Javascript code is being modified by Wordpress to include '<p>' tags

Using the philantrophy theme, I incorporated sliders into a page using shortcodes and encountered an issue where p tags were automatically inserted into my javascript code. The following code snippet highlights the unwanted p tag added in my javascript: ...