Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get optgroup label in a Bootstrap Select

Tags:

html

jquery

I have BootStrap select box and I am trying to find the optgroup of the selected option.

Here's a representation of a bootstrap select box with these options:

<li rel="1">
  <div class="div-contain">
     <div class="divider"></div>
  </div>
  <dt>
     <span class="text">Option Set 1</span> <!-- I NEED THIS: Option Set 1 -->
  </dt>
  <a tabindex="0" class="opt " style="">
    <span class="text">Option 1</span>
    <i class="fa fa-ok icon-ok check-mark"></i>
  </a>
</li>

<li rel="2" class="selected">
  <a tabindex="0" class="opt " style="">
    <span class="text">Option 2</span>
    <i class="fa fa-ok icon-ok check-mark"></i>
  </a>
</li>

EDIT: My recent try based on @Soren Beck Jensen answer:

$('i.check-mark').closest('li.selected').find('dt>span').text();

This gives me 'Option Set 1' for all and any options I select in the first dropdown.

like image 747
Rishi Jasapara Avatar asked Apr 21 '15 12:04

Rishi Jasapara


People also ask

Can Optgroup be selected?

From Select2 docs: "Furthermore, <optgroup> elements cannot be made selectable. This is a limitation of the HTML specification and is not a limitation that Select2 can overcome."

How do I use Optgroup tags?

The <optgroup> tag is used to group related options in a <select> element (drop-down list). If you have a long list of options, groups of related options are easier to handle for a user.

What is the specific purpose of the Optgroup HTML tag in creating a drop down list?

HTML <optgroup> Tag. This tag is used to create a group of the same category options in a drop-down list. The <optgroup> tag is required when there is a long list of the item exists.

What is the use of bootstrap select?

Bootstrap Select is a form control that shows a collapsable list of different values that can be selected. This can be used for displaying forms or menus to the user.


1 Answers

Currently your example is wrong, but try this:

$('li.selected').find('dt').find('span').text();
like image 127
Søren Beck Jensen Avatar answered Sep 20 '22 00:09

Søren Beck Jensen