Skip to content Skip to sidebar Skip to footer

How To Get All Optgroup Options Values And Push Them To An Array?

I have a dropdown that contains 2 optgroup inside it (Fiddle). What I'm trying to do now is, I want to grab all options values inside the Around The World Kitchens optgroup and pus

Solution 1:

Well, select has two optgourp one is Default Kitchen and another is Around The World Kitchens.

If you run selector only on optgourp it'll take both group in consideration.

If you wanna get value from Around The World Kitchens , you have mention it specificly

Like this

optgroup[label="Around The World Kitchens"]

This will try to find optgroup with label Around The World Kitchens

Try like this

$('#daily_order_kitchen_id optgroup[label="Around The World Kitchens"] option').each(function () {
    arr.push($(this).val())
});

JSFIDDLE

Solution 2:

Edit: Try This code.

var selectedKitchen = $("#daily_order_kitchen_id option:selected").val();
    var arr = [];
    $('#daily_order_kitchen_id option').each(function () {
        if($(this).parent().attr('label') != 'Around The World Kitchens') {
                arr.push($(this).val());    
        }       
    });
    console.log(selectedKitchen);
    console.log(arr);

Post a Comment for "How To Get All Optgroup Options Values And Push Them To An Array?"