Pass the value of a DropDownList to a JavaScript function

0

I want to pass the value that was selected from a list to a function when that value changes

This is the list in question:

<asp:DropDownList ID="DdlLista" runat="server" Width="290px" onchange="Alertando();"></asp:DropDownList>

and I must do some things in this function:

function Alertando(VALOR) {
if (VALOR=VALOR ESPERADO) {
alert('Se ha seleccionado VALOR como valor esperado');
}
else {
alert('Se ha seleccionado OTRO VALOR como valor no esperado');
}
}

what should I do?

    
asked by Joam Delgado 14.07.2017 в 18:00
source

1 answer

1

Pass a this to the onchange function

<asp:DropDownList ID="DdlLista" runat="server" Width="290px" onchange="Alertando(this);"></asp:DropDownList>

In your script you can get the value as follows

function Alertando(elemento){
    alert(elemento.value);
}

Edited

Try getting the element by its id

<asp:DropDownList ID="DdlLista" runat="server" Width="290px" onchange="Alertando();"></asp:DropDownList>

function Alertando(){
    var slt = document.getElementById('DdlLista');  
    var valor = slt.options[slt.selectedIndex].value;
    alert(valor);
}
    
answered by 14.07.2017 / 19:25
source