Update a label with dropdown text

1

In my label all groups appear:

  

ABC

There should only be one group chosen by dropdown, Example:

  

A

JS:

  $("#GrupoID").change(function () {

            document.getElementById("grupoupdate").innerHTML = $("#GrupoID").text();

        });

Label:

  @Html.Label((string)@ViewBag.Letra, htmlAttributes: new { @class = "form-control", @for = "grupoupdate", @id="grupoupdate" })

Dropdown:

 @Html.DropDownListFor(model => model.GrupoID, new SelectList(string.Empty, "GrupoID", "LetraGrupo"), "" , htmlAttributes: new { @class = "form-control" })

If you do document.getElementById("grupoupdate").innerHTML = $("#GrupoID").val(); , the correct Id appears in the label.

    
asked by anonymous 21.03.2016 / 14:03

1 answer

1

To get the text from label use the following selector with jQuery:

$("#GrupoID option:selected").text();

And to assign this text to your label :

$("#grupoupdate").text($("#GrupoID option:selected").text());

The pseudo-class selected gets the text of the selected item in select , that is, its DropDownList .

    
21.03.2016 / 15:49