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

145
Visualizações
How do get the result from jQuery change method?

This shouldn't be so difficult... A little rusty with Javascript and jQuery and I think I'm missing something obvious.

Code sample. Abbreviated DOM

<select data-drupal-selector="edit-country-code-country" id="edit-country-code-country" name="country_code[country]"
    class="form-select is-empty">
    <option value="" selected="selected">- None -</option>
    <option value="AF">Afghanistan</option>
    <option value="AL">Albania</option>
    <option value="ZW">Zimbabwe</option>
    <option value="AX">Åland Islands</option>
</select>

Code from script (doesn't work)

$('select[id^="edit-country-code-country"]').on('change', () => {
      const $target = $('#opt-in-wrapper');
      const countryNames = drupalSettings.r1;
      const $countrySelected = $('#select2-edit-country-code-country-container').attr('title');

      const countryCode = this.value;
      const test1 = $(this).value;
      const this2 = $(this).change;
      const this3 = $(this).data();

      const elem = $(this);
      const test5 = elem.value;
      const test6 = elem.value();

What I don't understand is why this.value returns undefined in the script but works as I would expect in the debug console

Debug results screenshot.

enter image description here

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

As you used arrow function in the scope of checkCountryChange, I think this belong to upper scope inside it so Replace arrow function with regular function to fix the issue.

$('#edit-country-code-country').on('change', function(){
    alert(this.value);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


    <select data-drupal-selector="edit-country-code-country" id="edit-country-code-country" name="country_code[country]"
        class="form-select is-empty">
        <option value="" selected="selected">- None -</option>
        <option value="AF">Afghanistan</option>
        <option value="AL">Albania</option>
        <option value="ZW">Zimbabwe</option>
        <option value="AX">Åland Islands</option>
    </select>

about 4 years ago · Santiago Gelvez Relatório

0

If the element has id, you can just use the id for the query selector. Then, you can find the selected option.

$('#edit-country-code-country').change(function() {
    var $option = $(this).find('option:selected');
    var code = $option.val(); // to get selected country code
    var name = $option.text(); // to get selected country name
});
about 4 years ago · Santiago Gelvez 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