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

310
Visualizações
What is the difference between xxx.value and xxx.options[xxx.selectedIndex].value to get the value of selected option with JavaScript?

After some research and testing, I figured out both methods produce the same result. So I was just wondering what the difference is between:

function buildUrl() {
  compType = document.querySelector('[name = "c-type"]');
  compTypeValue = compType.value;

}

and

function buildUrl() {
   compType = document.querySelector('[name = "c-type"]');
   compTypeValue = compType.options[compType.selectedIndex].value;

}
<form id="custom-drop">
  <select name="c-type" id="compressor-type">
    <option value="screw">Screw</option>
    <option value="scroll">Sroll</option>
    <option value="centrifugal">Centrifugal</option>
    <option value="piston">Piston</option>
  </select>
</form>

I did read questions (this and this) related to this topic but I couldn't find any explanation for their differences.

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

0

The select element keeps track on as well the value of the currently selected item and of the index of the selected items.

In your first example you access value of the currently selected option. In the second one you get the whole option element takeing the one at the index-postion that is selected. By accessing its value-member you then return its value.

By this you get the same result. However i would call the second approach hacky and not consider doing it.

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