0

I have in a page more partial views rendered. This is how the page looks: enter image description here

I need to handle to onclick event for the button Create Invoice

 <div class="row">
    <div class="button-bar col-md-2">
        @Html.ActionLink(@Resources.Common.ShowInvoice, "EditLotDamage", "TimberMonitor", new { Id = 0, LotId = ViewBag.Id }, new { @class = "btn btn-success" })
    </div>

    <div class="button-bar col-md-2">
        @Html.ActionLink(@Resources.Common.CreateInvoice, "EditLotDamage", "TimberMonitor", new { Id = 0, LotId = ViewBag.Id }, new { @id = "InvoiceDamageButton", @class = "btn btn-success", onclick = "validate" })
    </div>
</div>

The buttons are on a partial view with name: LotDamageButtonBarPartial, which is rendered from another partial view like this:

........
tabDamage.SetContent(() =>
    {
        if (ViewBag.CanModify)
        {
            Html.RenderPartial("LotDamageButtonBarPartial");
        }
        Html.RenderAction("LotDamageListPartial", new { LotId = ViewBag.Id });
    });
   ........

I have tried to handle the onclick event like this in the main page:

 $(document).ready(function () {

        $('#InvoiceDamageButton').click(function ()
        { 
            alert("test");
        });
});      

but it is not working. Can you advise?

Kar
  • 281
  • 1
  • 5
  • 15
  • 1. You are using more than just vanilla js. 2. Use dynamic binding [Why is this jQuery click function not working?](https://stackoverflow.com/questions/18602331/why-is-this-jquery-click-function-not-working) – Justinas Apr 27 '18 at 11:34

1 Answers1

0

When you are creating InvoiceDamageButton button, you are also defining an onclick handler for it here: onclick = "validate".

I don't know the code of validate() but it is probably stoping event propagation and the event never triggers your jQuery function.

Check if there's a e.stopPropagation(); inside function validate, if there is then remove this line. Also, check if validate() is returning false and if it is, change that too (either don't return anything or return true).

Guilherme Lemmi
  • 2,393
  • 6
  • 23
  • 28