Related Articles

Related Articles

How to add image before optgroup label using Bootstrap ?
  • Last Updated : 29 Jul, 2020

What is optgroup label?

It is an HTML tag that is used to group the options given in <select> tag. This combines the options in different groups having different labels.

Syntax:

filter_none

edit
close

play_arrow

link
brightness_4
code

<optgroup label="text">
  <option value="One">One</option>
  <option value="Two">Two</option>
</optgroup>

chevron_right


If we need to add an image before optgroup label using bootstrap than we have to put “image” tag in label giving the source location of the image with the label name.

Example: The following example demonstrates the optgroup having image before label using Bootstrap.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html lang="en">
  
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1
        shrink-to-fit=no">
  
    <script src=
    </script>
  
    <script src=
    </script>
  
    <link rel="stylesheet" href=
  
    <script src=
    </script>
  
    <link rel="stylesheet" href=
  
    <script src=
    </script>
  
    <style>
        .bootstrap-select img {
            width: 16px;
        }
    </style>
</head>
  
<body>
    <select class="selectpicker">
        <optgroup label=
            <option value="1">One</option>
            <option value="2">Two</option>
            <option value="3">Three</option>
        </optgroup>
        <optgroup label=
            <option value="Four">4</option>
            <option value="Five">5</option>
            <option value="Six">6</option>
        </optgroup>
    </select>
  
    <script>
        $('.selectpicker').on('shown.bs.select', function () {
            $('.bootstrap-select .dropdown-header').each(function () {
                if ((this.dataset.unescaped || '1') == '1') {
                    let element = $(this);
                    element.html(element.text());
                    element.attr('data-unescaped', '0');
                }
            })
        })
    </script>
</body>
  
</html>

chevron_right


full-stack-img




My Personal Notes arrow_drop_up
Recommended Articles
Page :