-1

I am trying to copy or put the text option selected on the dropdown list to text area Jquery on MVC View and I select by the text area doesn't show the text.. Any help??

 <div class="form-group">
            @Html.Label("LocationText", "Location Letter Text ", htmlAttributes: new { @class = "control-label col-md-2" })
            <br />
            @Html.DropDownList("RemarksId", null, new { id = "RemarksId", @class = "form-control" })
            <div class="col-md-10">
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-10">
                @Html.LabelFor(model => model.Remarks, htmlAttributes: new { @class = "control-label col-md-2" })
            </div>
            @Html.TextAreaFor(model => model.Remarks, new { @class = "form-control", id = "Remarks", rows = 5, cols = 70 })
            @Html.ValidationMessageFor(model => model.Remarks, "", new { @class = "text-danger" })
        </div>

JQuery

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script type="text/jscript">
      $(function () {
      $('#RemarksId').change(function()  {
      var str= $("#RemarksId option:selected").text();
        $('#Remarks').val(str);
      });
    });
  </script>
}
3
  • what scripts are in your bundle? you may need to move the jquery tag to above the bundle (or remove it completely if it is included in your bundle)
    – Pete
    Commented Oct 17, 2016 at 15:19
  • Your script urls may be wrong, could you check them ? Because your code looks correct by the way you don't need to write change function into the $(function()...) Commented Oct 17, 2016 at 15:29
  • what urls can I use?/
    – Marimar
    Commented Oct 17, 2016 at 16:03

1 Answer 1

1

You need to take the value of selected option from dropdown using .text method. please refer below code snippet

$(function () {
      $('#RemarksId').change(function()  {
      var str= $(this).val();
        $('#Remarks').val(str);
      });
    });

And, from the dropdown options change option value="1" to option id="1"

Do this for all options, and the above code will work for you.

Also see demo fiddle here Fiddle

Let me know if it works for you or not.

Thanks

18
  • actually the jquery in the question should work fine, even if it is a bit inefficient. You have changed the function to get the value rather than the text - are you sure this is what the OP wants?
    – Pete
    Commented Oct 17, 2016 at 15:32
  • According to the description of the question, it seems that he is having issue applying the logic on elements, which is based on jQuery, so I made some changes and provided snippet with demo, I thought it might help him to resolve the problem or atleast give him right direction.. Commented Oct 17, 2016 at 15:40
  • No. Doesn't work. :(
    – Marimar
    Commented Oct 17, 2016 at 16:03
  • can you tell me what error are you getting.. or any more info which you can provide.. Commented Oct 17, 2016 at 16:04
  • No error. Only, it doesn't show the select value on the text area
    – Marimar
    Commented Oct 17, 2016 at 16:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.