How to know which select option was selected

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 only returns the value of the first option.

I need to know when "Higher Education" is selected

    
asked by anonymous 15.12.2015 / 19:27

3 answers

4

Type something like this:

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

You could still do straight into javascript:

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

conceptName = box.options[box.selectedIndex].text;
    
15.12.2015 / 19:28
1

It could include a value for each option such as

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

To receive the text You can use

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

link

    
16.12.2015 / 12:49
0

For the purposes of helping others, follow the complete example:

$(document).ready(function () {

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

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

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

        alert(esValor);
    });

});
    
15.12.2015 / 19:35