How to know which select option has been selected

Asked

Viewed 10,288 times

3

I have a <select> and I need to know which <option> was selected.

<select class="formacao">
        <option>Ensino fundamental incompleto</option>
        <option>Ensino fundamental completo</option>
        <option>Ensino médio incompleto</option>
        <option>Ensino médio completo</option>
        <option>Ensino Superior</option>
</select>

I tried to solve here but jQuery just returns me the value of the first option.

I need to know when the "Higher Education" option is selected

  • Correcting there "But the jQuery"

  • 2

    Possible duplicate: http://answall.com/questions/95778/pega-select-selected%C3%B3-est%C3%A1-catching-first-value

3 answers

5

Like something like that:

var conceptName = $('#aioConceptName').find(":selected").text();

You could still do it direct in javascript:

var box = document.getElementById('aioConceptName');

conceptName = box.options[box.selectedIndex].text;
  • Thank you very much. You don’t know how you helped me.

  • tranquil @Alan mark the question as resolved

1

Could include a value for each option as for example

    <select class="formacao">
            <option value="1">Ensino fundamental incompleto</option>
            <option value="2">Ensino fundamental completo</option>
    </select>

To get the text You can use

    //caso estivesse no primeiro item
    $('.formacao option:selected').text() // para texto (Ensino fundamental incompleto)
    $('.formacao').val() // para valor (1)

https://learn.jquery.com/using-jquery-core/faq/how-do-i-get-the-text-value-of-a-selected-option/

0

For the purpose of helping others, follow the full example:

$(document).ready(function () {

    $('#formacao').change(function () {

        var es = document.getElementById('formacao');

        esValor = es.options[es.selectedIndex].value;

        alert(esValor);
    });

});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.