Select html getting value from a Javascript textbox

0

How to make a "Select" in html receive the value of a texbox and display the option according to what is typed in the textbox?

My code so far:

<html>
<head>
<script type="text/javascript">
    function teste(){
        var e = document.getElementById("EmpNomeIni");
        var itemSelecionado = e.options[e.selectedIndex].value;
        var txtEmpIni = document.getElementById("EmpIni");

        if(itemSelecionado == 1)
        {
            txtEmpIni.value = "01";
        }
        else if(itemSelecionado == 2)
        {
            txtEmpIni.value = "02";
        }
        else if(itemSelecionado == 3)
        {
            txtEmpIni.value = "03";
        }
    }

    function MudaValDDLComTXT(){
                var e = document.getElementById("EmpNomeIni");
                var itemSelecionado;
                var txtEmpIni = document.getElementById("EmpIni");

                if(txtEmpIni.value == "01")
                {
                    itemSelecionado = 2;
                    e.options[e.selectedIndex].value = itemSelecionado;
                }
            }
</script>
</head>
<body>

<div id="dados">
                        <label for="EmpIni">Empresa Inicial: <br>
                            <input type="text" id="EmpIni" name="EmpIni" onKeyPress="" class="" onmouseover="" onmouseout="" title="Código da Empresa" maxlength="02" onblur="MudaValDDLComTXT();"  value="">
                        </label>
                        <br>
                        <label for="CodTranspIni">Desc. Empresa Inicial:<br>
                            <select name="EmpNomeIni" id="EmpNomeIni" style="width:110px;" class="" onchange="teste();">
                                <option value="1">Empresa 1</option>
                                <option value="2">Empresa 2</option>
                                <option value="3">Empresa 3</option>
                            </select>
                        </label>
                        <br>    
</div>

</body>
</html>
    
asked by anonymous 07.06.2016 / 19:22

1 answer

0

Try changing your MudaValDDomomTXT method to something like this

function MudaValDDLComTXT(){
    var txtEmpIni = document.getElementById("EmpIni");
    $("#EmpNomeIni").val(txtEmpIni.value);
}
    
07.06.2016 / 19:30