javascript linking onclick to a .js to a file

Karl picture Karl · Feb 24, 2011 · Viewed 28.1k times · Source

I created a .js file, and then included it in the HTML pages by:

<script type="text/javascript" src="yourexternalfile.js"></script>

How do I call the function of the .js file using onclick = "...." ?

I know that it will be something like:

<input type="BUTTON" value="Exit" onclick="javascript: ???;" >

but I can't figure it out...

Answer

Furqan Hameedi picture Furqan Hameedi · Feb 24, 2011

If you are using JQuery, you can do it like this,

       <input type="button" onclick="javascript: $.getScript('../scripts/yourfilepath' , function (){ youFunctionCall() ; } );" />

This will download the JS file when the button is cliked and shall execute the function called within.