skip to Main Content

Hey I’m a complete beginner at javascript.

I have generated codes for a model website that uses html, css and javascript with chat gpt.

I have properly linked my html code with css and javascript, but the effects that I anticipate from my javascript don’t get applied to the html code.

Can you review my code and tell me why?

https://github.com/ilwoongchoi/javascripttest

above is my github repository for my model project.

Thanks

2

Answers


  1. Try this:

    function toggleNavbar() {
        var navbarLinks = document.getElementById("navbarLinks");
    
        var computedStyle = getComputedStyle(navbarLinks);
    
         if (computedStyle.display !== "none") {
            navbarLinks.style.display = "none";
        } else {
            navbarLinks.style.display = "block";
        }
    }
    

    The Window.getComputedStyle() method returns an object containing the values of all CSS properties of an element, after applying active stylesheets and resolving any basic computation those values may contain.

    Login or Signup to reply.
  2. Can you give more details, maybe which behavior you are looking for ? currently it’s working , just issue is that on resizing it’s each time calling your function , so why it blinking…
    For any case I think you can reach to this with css (there is meta tags in css which can allow to apply the other styles when the screen is smaller then expected)

    Also in any case you have already declared the same function in the first line so you can just give the reference to the addEventListener

    function toggleNavbar() {
        var navbarLinks = document.getElementById("navbarLinks");
        if (navbarLinks.style.display === "block") {
            navbarLinks.style.display = "none";
        } else {
            navbarLinks.style.display = "block";
        }
    }
    
    window.addEventListener("resize", toggleNavbar);
    

    Also be sure every time to check browser console to exclude the errors (related to JavaScript)

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