Changing the contents of a <optgroup> label when selecting an option in another select

Asked

Viewed 581 times

0

I would like to click, for example in the option "10 People"...

inserir a descrição da imagem aqui

It changed the label of several other selects, for example, this: "Special Fillings: R$10". I would like, for example, it to become "Special Fillings: R$20". When I choose "30 People", I switch to "Special Fillings: R$40".
inserir a descrição da imagem aqui

Here’s a part of my code: (I’m using the Select2 plugin, but I don’t know if it’s relevant to this issue)

I believe it is a solution VERY SIMILAR to this, but that instead of changing the content of <div>, change the one from <optgroup label="">: Link to similar solution

<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>

2 answers

1


Try with jQuery. Add a class to optgroup and try to change the label in the selection field change event.

html

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

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');
    }
    // ...
  });
});
  • Works like a Charm :) THANK YOU!

0

ONCHANGE-BASED CHANGE

A function activated in onChangecan be handled as follows:

function mudar(val){
  document.getElementById("mudar").label = "Novo Tipo de Bolo "+val;
}
<p><select onchange="mudar(this.value)">
  <option value='1'>Opção 1</option>
  <option value='2'>Opção 2</option>
  <option value='3'>Opção 3</option>
  <option value='4'>Opção 4</option>
  <option value='5'>Opção 5</option>
</select></p>

<p><select>
  <optgroup id="mudar" label="Tipo de Bolo">
  <option value='1'>Bolo 1</option>
  <option value='1'>Bolo 1</option>
  <option value='1'>Bolo 1</option>
  <option value='1'>Bolo 1</option>
  <option value='1'>Bolo 1</option>
</select></p>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.