Update a label with dropdown text

Asked

Viewed 19 times

1

On my label appears all groups:

ABC

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

To

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 on the label.

  • The id that reaches the script is correct. " Grupoid"

1 answer

1


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

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

And to attribute such a 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, in its DropDownList.

  • Does not work. Enters function, but does not modify value.

  • Okay, it works. It was my syntax error.

Browser other questions tagged

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