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

142
Visualizações
How do I access an <option> using its value?

I have the following code:
HTML

<select name="cf[source]" id="form1_source" required="" class="cf-input cf-input-shadow-1 cf-one-half ">
    <option value="notspecified" data-calc-value="notspecified">Please select</option>
    <option value="Facebook" data-calc-value="Facebook">Facebook</option>
    <option value="Flyer" data-calc-value="Flyer">Flyer</option>
    <option value="Instagram" data-calc-value="Instagram">Instagram</option>
</select>

Javascript

document.addEventListener('DOMContentLoaded', function(){
    var source = document.getElementById('form1_source');
    var ref = window.location.search;
    var IG = source[source.options.length-1];
 });

I want to be able to always access the "Instagram" option to do things with it. It's currently the last in the list, so I'm using the length of the dropdown to access it, but I don't want to have to update the code every time I add something to the list, before or after Instagram. Is there a way to access the <option> code using something like source.options['Instagram'] using Javascript (not jQuery)? I can't assign ids to the options, because the options are generated using other software that doesn't give me the option to do that. I'm also trying to avoid looping through the options comparing the values with what I'm looking for.

EDIT: The user is choosing how they heard about my site. If they come to the site link from IG, then the URL has ?ref=IG appended to it. I want all the other options to disappear since that's the link they followed.

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

0

You can use a regular query selector.

let select  = document.getElementById('form1_source');
let options = select.querySelectorAll('option[value="Instagram"]');
console.log(options[0]);
<select name="cf[source]" id="form1_source" required="" class="cf-input cf-input-shadow-1 cf-one-half ">
    <option value="notspecified" data-calc-value="notspecified">Please select</option>
    <option value="Facebook" data-calc-value="Facebook">Facebook</option>
    <option value="Flyer" data-calc-value="Flyer">Flyer</option>
    <option value="Instagram" data-calc-value="Instagram">Instagram</option>
</select>

about 4 years ago · Juan Pablo Isaza Relatório

0

I want to ask for a clarification (I would add a comment but currently not enough rep).

A user chooses an option, therefore, why do you want to get a value that is there already?

Instead of targeting one option, to do whatever, I would expect the correct approach would be to assume you do not know what the user chooses.

<select name="cf[source]" id="form1_source" required="" class="cf-input cf-input-shadow-1 cf-one-half ">
    <option value="notspecified" data-calc-value="notspecified">Please select</option>
    <option value="Facebook" data-calc-value="Facebook">Facebook</option>
    <option value="Flyer" data-calc-value="Flyer">Flyer</option>
    <option value="Instagram" data-calc-value="Instagram">Instagram</option>
</select>
<p id="feedback">

</p>
<script>
 var source = document.getElementById('form1_source');
 source.addEventListener("change",function(event_object){
  var feedback = document.getElementById("feedback");
  feedback.innerHTML = event_object.target.value;
 })
</script>

Please check this fiddle: https://jsfiddle.net/hyurxtnz/3/ and update your question with additional information.

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