Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

255
Vistas
Show/hide div based on selected optgroup option that also works when editing

I have two optgroup. Several options on optgroup for Debit purposes and another for Credit Purposes. I'm trying to build a solution in Javascript to show me a div when I chose one of the Debit optgroup options and other div when I choose one of the Credit optgroup options. Nothing works! I question my self if this is the best solution once I need the code also works when I Edit the data.

Here is my code at the moment:

<select onchange="ChangeCategory(event)" type="text" name="rubrica" id="rubrica" class="form-control" required>
                      <optgroup id="debit" label="DESPESAS">
                      <?php
                      $result_rubrica = $dbcondo->query("SELECT * FROM lcs_tipo_rubrica WHERE tipo_rubrica='DÉBITO' order by rubrica ASC");
                      while($row_rubrica = $result_rubrica->fetch()){

                            $selected = "-- escolha uma opção --";

                            $output = '<option value="'.$row_rubrica['rubrica'].'" '.$selected.'>'.$row_rubrica['rubrica_id'].' '.$row_rubrica['rubrica'].'</option>';

                            echo $output;
                            }
                        ?>
                        </optgroup>
                        <optgroup id="credit" label="CREDITOS">
                          <?php
                          $result_rubrica = $dbcondo->query("SELECT * FROM lcs_tipo_rubrica WHERE tipo_rubrica='CRÉDITO' order by rubrica ASC");
                          while($row_rubrica = $result_rubrica->fetch()){

                                //if($row_rubrica['bill_type'] == $_GET['$rubrica']){
                                $selected = "selected";
                                //}else{
                                //  $selected = "-- escolha uma opção --";
                                //}
                                $output = '<option value="'.$row_rubrica['rubrica'].'" '.$selected.'>'.$row_rubrica['rubrica_id'].' '.$row_rubrica['rubrica'].'</option>';

                                //Echo output
                                echo $output;
                                }
                            ?>
                        </optgroup>
                        </select>

And the javascript

function ChangeCategory({ target }) {
if(selected && selected !== target.value) toggleDisplay(selected)
selected = target.value
return toggleDisplay(target.value, 'remove')
}
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda