Balance
Balance

Reputation: 167

External js script not working in View (ASP.NET)

I have external js script where for example I writing this code

    $('#save').click(function () {
    alert('Clicked');
});

In view I have button

 <div class="form-group" style="text-align:center">
    <input type="button" id="save" value="Create"  style="border-radius:30px; background: #1d69b4; border-color: #1d69b4;width: 250px; color: white;" class="btn btn-default" style="margin-right: 40px;" />
</div>

In header of View I put scripts like this

 <script src="~/Scripts/jquery-3.2.1.js"></script>
<script src="~/Scripts/HTMLCanvas.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/0.9.0rc1/jspdf.min.js"></script>
<script src="~/Scripts/PatientDatabaseScripts/AddingPatient.js"></script>

When I click button, it does nothing, any alerts. In dev console no errors.

But when I write alert via function , and in button put code onclick = "save()", all okay.

Where is my problem?

Upvotes: 0

Views: 477

Answers (1)

Nedim Hozić
Nedim Hozić

Reputation: 1901

It might be that you try to attach event handler on the DOM element which is not loaded yet.

The one thing that you can do is wrap your JS in a 'ready' block, like this:

$(document).ready(function(){
    $('#save').click(function () {
      alert('Clicked');
    });
})

Upvotes: 1

Related Questions