javascriptjquery-select2jquery-selectbox

Update optgroup label when I choose another select option


Here's what I need: When I choose an option like "10 Pessoas"...

Image One

I need to change the optgroup label on a few other select options like this one: Where I have the information "Recheios Especiais: R$10" I want to change, for example to "Recheios Especiais: R$20". When I choose "30 Pessoas", change the label to "Recheios Especiais: R$40".

Image Two

Here's my code: (Im using select2 plugin, but I don't think this is relevant for the question)

Guess the solution is VERY SIMILAR to this one, but instead of changing the content of a <div>, I need to change the content of a <optgroup label="">: (LINK IS ON THE COMMENTS)

<form class="form" method='post' id='formBoloMesa' action="custom_includes/orderBolo.php">

<div class="form-group">
    <span class="resume-subtitle">Escolha o tamanho</span>
    <select name="tamanho" class="form-control select2-list select-options-1" data-placeholder="Clique aqui para escolher" multiple>
        <optgroup label="Tamanho">
            <?php
                include ('custom_includes/connect.php');
                $strTamanho = "SELECT * FROM bolo_mesa_itens WHERE tipo='tamanho'";

                $rsTamanho = mysql_query($strTamanho);

                while($rowTamanho = mysql_fetch_array($rsTamanho)) {
                    echo '<option value="' . $rowTamanho['nome'] . '">' . $rowTamanho['nome'] . '</option>';
                } 
            ?>      
        </optgroup>                                                 
    </select>
</div>

<div class="form-group">
    <span class="resume-subtitle">
        Escolha o modelo
    </span>
    <select name="modelo" class="form-control select2-list select-options-1" data-placeholder="Clique aqui para escolher" multiple>
    <optgroup label="Tipo de Bolo">
    <?php
        $strModelo = "SELECT * FROM bolo_mesa_itens WHERE tipo='modelo'";

        $rsModelo = mysql_query($strModelo);

        while($rowModelo = mysql_fetch_array($rsModelo)) {
            echo '<option value="' . $rowModelo['nome'] . '">' . $rowModelo['nome'] . '</option>';
        }
    ?>
    </select>
</div>

<div class="form-group">
    <span class="resume-subtitle">Escolha a Massa</span>
    <select name="massa" class="form-control select2-list select-options-1" data-placeholder="Clique aqui para escolher" multiple>
        <optgroup label="Massas">
        <?php
            $strMassas = "SELECT * FROM bolo_mesa_itens WHERE tipo='massa'";

            $rsMassas = mysql_query($strMassas);

            while($rowMassas = mysql_fetch_array($rsMassas)) {

                echo '<option value="' . $rowMassas['nome'] . '">' . $rowMassas['nome'] . '</option>';

            }
        ?>
        </optgroup>                                         
    </select>
</div>

<div class="form-group">
    <span class="resume-subtitle">Escolha 3 recheios</span>
    <select name="recheio1" class="form-control select2-list select-options-1" data-placeholder="Escolha o 1º Recheio" multiple>
        <optgroup label="Recheios Comuns">
            <?php
                $strRecheios = "SELECT * FROM bolo_mesa_itens WHERE tipo='recheio'";

                $rsRecheios = mysql_query($strRecheios);

                while($rowRecheios = mysql_fetch_array($rsRecheios)) {
                    echo '<option value="' . $rowRecheios['nome'] . '">' . $rowRecheios['nome'] . '</option>';
                }
            ?>  
        </optgroup>
        <optgroup label="Recheios Especiais (+R$10)">
            <?php
                $strRecheiosPremium = "SELECT * FROM bolo_mesa_itens WHERE tipo='recheio' AND premium='1'";

                $rsRecheiosPremium = mysql_query($strRecheiosPremium);

                while($rowRecheiosPremium = mysql_fetch_array($rsRecheiosPremium)) {
                    echo '<option value="' . $rowRecheiosPremium['nome'] . '">' . $rowRecheiosPremium['nome'] . '</option>';
                }
            ?>
        </optgroup>                                                 
    </select>

Solution

  • Found an answer:

    JS

    $(document).ready(function () {
    $('select[name=tamanho]').change(function () {
    var selectRecheioGroup = $('select[name=recheio1] .grupoRecheio');
    var tamanhoSelecionado = $(this).val();
    
    if (tamanhoSelecionado == 'Tamanho1') {
      selectRecheioGroup.attr('label', 'Recheios Especiais: R$20');
    } else if (tamanhoSelecionado == 'Tamanho2') {
      selectRecheioGroup.attr('label', 'Recheios Especiais: R$30');
    }
    // ...
      });
    });
    

    HTML

    <optgroup class="grupoRecheio" label="Recheios Especiais: R$10">