Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

233
Visualizações
Javascript Returning values with Form Optgroup parent

I would like to return the option values selected in a multiple option select box It works it I do not have a optgroup tags in. As soon as I add in the these tags, I cannot get the child values. I am sure this is a noddy question but I cannot find the correct syntax to get the correct array which is dropdown.options

$('#mydropdown option').click(function(evt) {
  var selectedOption = evt.target;
  var dropdown = this.parentNode;
  queue.add(selectedOption.value);
  Array.from(dropdown.options).forEach(function(option) {
    if (!option.selected) {
      queue.remove(option.value);
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<select id="mydropdown" multiple="multiple">
  <optgroup label="Group 1">
    <option value="optionx Fruits" selected>Fruits</option>
    <option value="optionx Vegetables">Vegetables</option>
  </optgroup>
  <optgroup label="Group 2">
    <option value="optionx Nuts">Nuts</option>
    <option value="optionx Meats">Meats</option>
  </optgroup>

</select>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

We can just create a new set each time

This is important if you want the set to contain only selected ones.

If you want the set to contain what the user selected and KEEP it when the user unselect, then just add

Also notice I trigger the change to initialise the set when loading

let queue;
$('#mydropdown').on("change", function(evt) {
  queue = new Set();
  $("option:selected",this).each(function() { queue.add(this.value) });
  console.log("changed -------")
  for (const item of queue) console.log(item)
}).change();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<select id="mydropdown" multiple="multiple">
  <optgroup label="Group 1">
    <option value="optionx Fruits" selected>Fruits</option>
    <option value="optionx Vegetables">Vegetables</option>
  </optgroup>
  <optgroup label="Group 2">
    <option value="optionx Nuts">Nuts</option>
    <option value="optionx Meats">Meats</option>
  </optgroup>

</select>

IF you want to keep the order selected, you will indeed need to just add and then remove

let queue = new Set();

$('#mydropdown').on("click", function(evt) {
  $(this).find("option").each(function() {
    queue.add(this.value); 
    if (!this.selected) queue.delete(this.value)
  })
  console.log("changed -------")
  for (const item of queue) console.log(item)
}).click();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<select id="mydropdown" multiple="multiple">
  <optgroup label="Group 1">
    <option value="optionx Fruits" selected>Fruits</option>
    <option value="optionx Vegetables">Vegetables</option>
  </optgroup>
  <optgroup label="Group 2">
    <option value="optionx Nuts">Nuts</option>
    <option value="optionx Meats">Meats</option>
  </optgroup>

</select>

about 4 years ago · Juan Pablo Isaza Relatório

0

On click your parentnode becomes optgroup

var dropdown = this.parentNode;

so you should set

var dropdown = this.parentNode.parentNode;
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda