Use HTML Button OnClick to call a JavaScript function in another file

How do I include an external JavaScript file so that a HTML Button OnClick has access to it?

Please provide an example.

Thank you
AmbassadorSpockAsked:
Who is Participating?
 
Mohit VijayConnect With a Mentor Commented:
assume you have a javascript file called myfile.js @ the root of application

so you have to give ref. of that fine in head tag of html

<script src="~/myfile.js"></script>

now in this myfile.js, you can write a function that will be called on click on html button. assume the function name is buttonClicked

so in js file, you can write

function buttonClicked()
{
alert('button click called');
}

And for html button you can write

onclick="javascript:buttonClicked();"
0
 
AmbassadorSpockAuthor Commented:
Great....thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.