Home > Back-end >  JS onclick function passing external file
JS onclick function passing external file

Time:12-03

I have a menu which is plain HTML In there I have some images. When you click on those an action should be preformed (for example opening or closing the menu). This plain HTML menu is included by PHP.

To preform an action, I thought adding the JS onclick="" tag to my HTML would be a good idea. Now I have created some .js files that are in a subdirectory called js.

Now is my question how to pass the .js files located in the subdir to the onclick="" tag.

(TLDR)

/index.php <- there is the navigation bar inside

/js/file.js <- want to access this file via onclick="" in index.php

How can I do that

CodePudding user response:

Your question is very dubious. All you have to do is to put this <script src="/js/file.js"></script> before your </body> tag (or footer if you want) then call the functions inside with onclick or onload event. Provide more details if you need more clarification.

CodePudding user response:

If you're trying to access some JavaScript in an external file, just include that file in your index.php like this.

<script src="myscripts.js"></script>

Then you can call your JavaScript functions in the "onclick" method as if it was written on the same page.

onclick="yourMethod()" //your method name here 

Check this out: https://www.w3schools.com/tags/att_script_src.asp

  • Related