Enhance your user interface with Bootstrap Multi-select by incorporating images alongside checkboxes

First off, you can find the working example here

I'm attempting to insert an image after each checkbox for the Bootstrap multi-select plugin. I have tried a couple of methods so far:

  1. Adding images directly in each option, but the plugin removes all html.
  2. Using jQuery's after() method to add an image after the field, but it doesn't seem to work.

Here is the HTML code snippet:

<div class="col-sm-12">
    <div id="vm" class="tab-pane fade in  active">
        <div class="panel panel-primary">
            <div class="panel-body">
                <form action="/Lab/Rtx/etabs/edit/0451483t" id="ServerEditForm" method="post" accept-charset="utf-8">
                <div style="display:none;">
                    <input type="hidden" name="_method" value="POST">
                </div>
                <div class="col-md-8">
                    <div class="form-group">
                        <input type="hidden" name="data[Server][0][id]" class="form-control" value="1" id="Server0Id">
                        <div class="controls">
                            <label for="Server0Vm">hosta0451483t</label>
                            <input type="hidden" name="data[Server][0][Vm]" value="" id="Server0Vm_">
                            <select name="data[Server][0][Vm][]" class="multiselect" multiple="multiple" id="Server0Vm">
                                <optgroup>
                                    <option value="ctrlr0451483t">ctrlr0451483t</option>
                                    <option value="ipr0451483t">ipr0451483t</option>
                                    <option value="ldap0451483t">ldap0451483t</option>
                                    <option value="proxy0451483t">proxy0451483t</option>
                                </optgroup>
                            </select>
                        </div>
                    </div>
                </div>
            </div>
            </form>
        </div>
    </div>
</div>

And here is the jQuery code snippet:

$(document).ready(function() {
    $('.multiselect').multiselect({
        buttonWidth: 'auto',
        numberDisplayed:15,
        onChange: function(element, checked) {
            $("ul.multiselect-container").find("input[type=checkbox]").each(function(){
                var valueAttr = $(this).attr('value');
                if (element.attr('value') == valueAttr) {
                    var checkParent = $(this).parent().parent().parent().hasClass('active');
                    if (checkParent == false) {
                        $(this).removeAttr('disabled')
                    }else{
                        $(this).attr('disabled','disabled')
                    }
                }
            });
        }
    });
});

Any assistance on this matter would be greatly appreciated.

Answer №1

If you want to customize the labels of the options, you can utilize the optionLabel parameter

optionLabel is a callback function that allows you to define option labels dynamically.

$('.multiselect').multiselect({
    enableHTML: true,
    optionLabel: function(element) {
        return '<img src="'+$(element).attr('data-img')+'"> '+$(element).text();
    },
    // ...
});

Here's how your HTML structure should look like:

<select name="data[Server][0][Vm][]" class="multiselect" multiple="multiple" id="Server0Vm">
    <optgroup>
        <option data-img="1.png" value="ctrlr0451483t">ctrlr0451483t</option>
        <option data-img="2.png" value="ipr0451483t">ipr0451483t</option>
        <option data-img="3.png" value="ldap0451483t">ldap0451483t</option>
        <option data-img="4.png" value="proxy0451483t">proxy0451483t</option>
    </optgroup>
</select>

To see it in action, check out this JSFiddle demo

Answer №2

Here is a sample code snippet that you can use:

$(document).ready(function() {
  $('#mySelect').multiselect({
    includeSelectAllOption: true,
    enableFiltering: true
  });
});
.multiselect-container>li>a>label {
  padding: 4px 20px 3px 20px;
}
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/css/bootstrap-multiselect.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/js/bootstrap-multiselect.js"></script>

<select id="mySelect" multiple>
  <option value="1">Option 1</option>
  <option value="2">Option 2</option>
  <option value="3">Option 3</option>
  <option value="4">Option 4</option>
  <option value="5">Option 5</option>
  <option value="6">Option 6</option>
</select>

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

Check the dimensions of the image file entered using AngularJS on the client side

Before uploading an image to the server, I need to validate its dimensions on the client side. I have searched for solutions using img.Onload(), but that's not what I am looking for. All I want is for the user to choose the image from <input ...

Using jQuery: how can we animate an upward movement?

I currently have a vertical list of DIVs and I am able to use the .fadeOut() method to remove one of the middle DIVs. However, I am unsure how to generate a slow move-up effect for the remaining DIVs below the deleted one. Any advice on achieving this? ...

Can angular and grunt support multiple run blocks simultaneously?

Currently, I am configuring $httpBackend to simulate fake API routes while our team of API developers is building them out. However, I am facing an issue where I have to place all the $httpBackend configurations within my run block. This leads to a situa ...

"Enhance Your Online Shopping Experience with Woocommerce Ajax Filters and

I have a structure that looks like this: Company Google Apple Microsoft Material Wood Metal Plastic Essentially, Brand & Material are attributes in the Woocommerce system, while the rest are variables. I am currently working on implementing AJAX fi ...

What is the best way to include both left and right borders in HTML/CSS

I'm looking to add left and right borders to a specific cell within my table. Utilizing Bootstrap 4 beta, I attempted the following code snippet: <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="style ...

The app was rejected from the Play Store due to a breach of section 4.4 in the Developer Distribution Agreement

Just got an email notification that my application submission, Chami Browser, has been rejected due to violation of section 4.4 of the Developer Distribution Agreement. The reason for rejection is accessing YouTube videos in violation of their Terms of Se ...

How can we use jQuery to replace a u002D in a string obtained from an attribute

My goal is to extract the URL inside the "ng-init" attribute from this particular div element. <div ng-controller="CloudcastHeaderCtrl" ng-init="juno.replaceTracklist=true;juno.guid='DBFFF66E\u002D4111\u002D446B\u002D8471\u002D ...

The server returned a response that was void of any content

I have encountered an issue while trying to retrieve data from my server. The request works perfectly fine when tested with Postman, returning the expected data. However, upon implementing the request in my application, I receive an empty object with prope ...

"Exploring three.js: Transforming a plane's orthogonal vector into a rotation matrix for the plane

I am trying to set the rotation of a plane by using three numbers representing the rotation in radians along the x, y, and z axes. Although I do not have these numbers, I do have a vector called 'myVec' which should be orthogonal to the plane af ...

Minimize the quantity of files processed by Vue

Here is the content of my vue.config.js file: css: { extract: false, }, configureWebpack: { devtool: 'source-map', optimization: { splitChunks: false, }, }, productionSourceMap: false, I'm facing an issue where running npm run b ...

I can't figure out why the data in this JSON string keeps altering within my ajax function

I am encountering an issue with passing an array of JSON objects to my controller. Despite appearing well-formed and correctly populated when I insert an alert before the ajax call, some variables are showing null values when inspecting the payload of the ...

Navigating Google GeoChart Tooltips using Google Spreadsheet Data

My objective is to create a custom GeoChart for the company's website that will display data specific to each state in the US region based on team member assignments. The GeoChart should color states according to which team member helps that state&apo ...

Is there a way to upload a kml file to Google Maps using my website or by using JavaScript commands?

I have information on my website regarding gas stations, including the quality of gasoline and the GPS coordinates of each station. My concept involves incorporating Google Maps into my site, similar to how flightradar24.com displays pins indicating gas s ...

[Assistance Needed]: Transforming Three.js coordinate systems

Is there a way to adjust the default drawing position in three.js from (0,0,0) at the center of the canvas to the actual start of the canvas? For an example, check out this code: https://jsfiddle.net/data_x/mwprgnra/8/ I am looking to shift the lines to ...

Bringing tex2max.js into an angular application using npm and index.html

Every time I attempt to import tex2max with the command declare var tex2max: any;, I encounter a ReferenceError: tex2max is not defined. Interestingly, this issue does not arise with other npm packages. Despite trying various methods such as installing the ...

I'm encountering an issue: Uncaught ReferenceError

I am currently working on a to-do list project in javascript, but I keep encountering an error message that says: Uncaught ReferenceError: itemsjson is not defined. After setting my get.item to null, I proceeded to add .push to the code. Can someone pleas ...

Fixed Collapse of Vertical Navbar in Bootstrap 4

Currently delving into the world of bootstrap and facing a challenge. I am attempting to craft a vertical navbar on the left side of the page once the user has scrolled past the full-page 'intro' element using bootstrap 4. The code snippet provi ...

Selecting multiple values using Ajax in a select2 dropdown menu

I am currently using Select2 and fetching data in JSON format via AJAX. View: <select class="form-control qual_id" multiple> <option value="">-Select Degree-</option> <option value="1">SSC</option> <option val ...

Tips for incorporating animated features into a bar graph using jQuery

How can I create a dynamic animated bar graph using jQuery? I am looking to make the bar slide up from the bottom, incorporating images for both the bar and background. I have already set the positioning in CSS and now need to add animation to make the bar ...

The Regex.Replace function is not functioning as expected

I am currently faced with the challenge of replacing HTML tags within a string. For example, I need to convert <strong>text</strong> to <b>text</b>, among other similar replacements (although I understand that the b tag is considere ...