Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get option group of selected item on .chosen().change()

For a simple option grouping, say:

   <optgroup label="fruit">
     <option value="1"> apples </option>
     <option value="2"> pears </option>
  </optgroup>
  <optgroup label="veg">
     <option value="3"> neeps </option>
     <option value="4"> tatties </option>
  </optgroup>

I'm able to get grab the selected option's id... Using:

    $('#my-chzn').chosen().change(
        function(evt) {
           var id = $(this).val();
           // ...or 
           var id_ = $(evt.target).val();
        }
    );

But is it possible to grab the <optgroup> label for a selected option? ie is there a handle/selector to grab the value 'fruit' when the selected option is 'pears'?

Many thanks for any help anyone's able to offer....

like image 751
user908094 Avatar asked Jun 28 '13 09:06

user908094


People also ask

What element can be used to group options in a selection list?

<optgroup>: The Option Group element.

How do you select a particular option in a select element in Javascript?

Use the selectedIndex and value to get the index and value of the selected option. The HTMLOptionElement represents the <option> element. If the option is selected, the selected property is true. The selectedText and selectedValue properties return the text and value of the selected option.

What is optiongroup object in access?

OptionGroup object (Access) An option group on a form or report displays a limited set of alternatives. An option group makes selecting a value easy because you can choose the value that you want. Only one option in an option group can be selected at a time.

How do I set the value of an option group?

When you select an option in an option group, Microsoft Access sets the value of the field to which the option group is bound to the value of the selected option's OptionValue property. Note The OptionValue property is set to a number because the value of an option group can only be a number, not text.

How to retrieve the selected option value from a <select> element?

This creates a list for the user to select from. Retrieving the selected option value can be done using the following code sample: Retrieving the selected value from a <select> element is done using the value attribute In the above code, the HTML document has a <select> element. Its id is set to greet, and it has three options to choose from.

How to get selected option value using jQuery select change event?

You can use this jquery select change event for get selected option value $ (document).ready (function () { $ ('body').on ('change','#select', function () { $ ('#show_selected').val (this.value); }); });


1 Answers

You can accomplish what you want as the code is shown below

$('.chosen').chosen().change(
    function (evt) {
      var label = $(this.options[this.selectedIndex]).closest('optgroup').prop('label');
      alert(label);
});

Demo on fiddle

like image 164
agriboz Avatar answered Sep 29 '22 23:09

agriboz