skip to Main Content

main folder and html src, inside js folder
I’m trying to link my js file that is in a folder to my html file, but it doesn’t seem to be working. I’m creating a dropdown menu and using js to open the dropdown. The console log shows no errors, but none of the script is coming through. It works when I put the script into the html, but it needs to be in a separate file in a folder because it’s part of the instructions for the assignment. The file I’m linking to is the dropdown-menu file at the bottom. Let me know if I can make anything clearer.

I’ve tried putting the src in the header, in the body tag at the end, and just outside of the end of the body tag. I’ve also tried "js/dropdown-menu.js", "/js/dropdown-menu.js", "../js/dropdown-menu.js", but it didn’t make a difference either.
Any help would be appreciated, Thanks!

2

Answers


  1. You may use the script html tag with a fitting relative path.

    Login or Signup to reply.
  2. Maybe what is not working is your content of the .js file and not the reference to the file itself. What you can do for this is add a function so that clicking a button triggers an alert. This will help you know if it is a linking problem with the .js file or not.

    I share with you the example of the code that you must add in your dropdown-menu.js file and in your HTML:

    <!-- HTML file -->
    <button id="btn" >click me</button>
    
    // JS file
    button = document.getElementById('btn');
    button.addEventListener('click', () => {
      alert('button clicked');
    });
    

    If this code works then the error is inside dropdown-menu.js, maybe you should share it so I can review it too.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search