Select html receiving value from a Javascript textbox

Asked

Viewed 128 times

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>

1 answer

0

Try to change your method Mudavalddlcomtxt for something like this

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

Browser other questions tagged

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