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....
<optgroup>: The Option Group element.
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.
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.
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.
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.
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); }); });
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With