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

124
Vistas
Get selected optgroup in JavaScript

I have the below select tag with two optgroup.

<select id="linkSelector" class="form-control" onchange="linkDropDown();">
  <optgroup label="Input">
   <option value="default">Road type</option>
   <option value="lanes">Lanes</option>
   <option value="length">Length</option>
   <option value="speed">Speed</option>
  </optgroup>

  <optgroup label="Ouput">
   <option value="congestion">Congestion</option>
   <option value="speed_output">Speed</option>
   <option value="travel_time">Travel Time</option>
  </optgroup>
</select>

I would like to execute myFuncton() depending on the selected optgroup. For example if one of the dropdown value belong to <optgroup label="Input">, then execute myFunction().

I tried this:

var linkSelector = document.getElementById('linkSelector')
opt = linkSelector.getElementsByTagName('optgroup')
if (opt[0].label =='Input'){
    myFunction()
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

One way to go about this, would be to get the selected option for the select field, then find out it's parent's label.

var label = document.getElementById('linkSelector').selectedOptions[0].parentElement.label;
if (label === "Input") {
  myFunction();
}
about 4 years ago · Juan Pablo Isaza Denunciar
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