Search code examples
javascripthtmljquerycssmodel-view-controller

Sending parameter JQuery Function from Button


I have a button and click function as you see below. I wanna send @item.Id value as parameter in click function. How can I do that?

<button id="buttonEdit" style="color:darkgreen" data-toggle="modal" data-target="#EditRecord">@item.Id</button>

...

<script>
            $(document).ready(function () {
                $("#buttonEdit").click(function () {
    
                    $("#Id").val('parameter');
    
                });
            });
        </script>

Solution

  • Grab the text content with text(), and assign it to the value attribute of the appropriate element.

    $('#buttonEdit').click(function() {
      const parameter = $(this).text();
      $('#Id').val(parameter);
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <button id="buttonEdit" style="color:darkgreen" data-toggle="modal" data-target="#EditRecord">@item.Id</button>
    
    <input type="text" id="Id" />