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

201
Visualizações
can I set the select option using anything other than value in javascript

I have a pick list like the following:

<select multiselect="false" name="some.name" size="1" id="queuePicklist" 
    onchange="setQueue();" required="true">
    <option id="selectOption" hidden="true" disabled="true" selected="true" value="" 
        style="display: none">Select an option</option>
    <option value="1" label="Support">Support</option>
    <option value="2" label=" Team Sales">Team Sales</option>
    <option value="1" label="Individual Sales">Individual Sales</option>
    <option value="1" label="Billing">Billing</option>
    <option value="1" label="Other">Other</option> 
</select>

I want to know if it's possible that in setQueue() function select one of the options without using the value? So instead of document.getElementById("queuePicklist").value = 'Support';

Can I add a data attribute to the options and select the option that way so I can keep the values as they are. Note: as seen in the example four of the options have the same value.

I know I can put these values (1,2,1,1,1) into the data attribute and use unique values in the 'value' field and that was my first approach but since I'm working with some legacy code, that change made other parts of other codes to break.

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

0

I'm not exactly sure what or how you want to get an option, but you can access the text like this.

function setQueue() {
  var selectEle = document.getElementById("queuePicklist");

  // .options returns an HTMLOptionsCollection object.
  // use the selectedIndex property to get the selected option, then
  // you can access the text or value.

  console.log(selectEle.options[selectEle.selectedIndex].text);
}
<select multiselect="false" name="some.name" size="1" id="queuePicklist" onchange="setQueue();" required="true">
  <option id="selectOption" hidden="true" disabled="true" selected="true" value="" style="display: none">Select an option</option>
  <option value="1" label="Support">Support</option>
  <option value="2" label=" Team Sales">Team Sales</option>
  <option value="1" label="Individual Sales">Individual Sales</option>
  <option value="1" label="Billing">Billing</option>
  <option value="1" label="Other">Other</option>
</select>

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