jQuery not functioning properly when included in a separate JavaScript file

I'm facing an issue with my jquery code not working properly when I place it in a separate js file instead of embedding it directly into the html. Currently, my setup looks like this:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript" src="js/responsive.js" media="screen and (max-width: 480px)" defer</script>

Within the "responsive.js" file, my code is as follows:

document.getElementById("navbar").style.display = "none" {
  $('#navbar').hide()
}

Answer №1

In order to find the solution, it is important to refer to the documentation available at: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script

The media attribute is not supported for the <script> tag as indicated in the provided resource. It is essential to adhere to the standard attributes and not create custom ones.

If your file named 480.js consists only of the mentioned content, it would be advisable to remove it since it contains invalid JavaScript which can lead to errors.

To resolve the issue, JavaScript is not required. The desired result can be achieved using pure CSS. (Refer to this fiddle: https://jsfiddle.net/u19s432z/)

Add the following CSS code:

#navbar {
    display: block;
}

@media screen and (max-width: 480px) { 
    #navbar {
        display: none;
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Mobile browser not resizing window width in fluid layout on WordPress site

Currently, I am working on developing a basic web layout that can adapt to mobile view. I have made some progress which you can check out here. The layout successfully adjusts when the window is resized in Firefox or Chrome. However, when I tried to acce ...

Displaying a mouse cursor using an A-frame function

I am presently working on a scene using A-frame () in which I have hidden the mouse pointer. Is there a way for me to set it up so that when a specific function is triggered, the mouse pointer will show, and when another function is executed, the mouse poi ...

Creating three boxes within a single div using standard HTML and CSS

I created this design using Microsoft Paint and now I want to bring it to life using HTML and CSS. https://i.stack.imgur.com/dwNZi.png The numbers represent the box numbers in the design. Here is my attempt at coding this: HTML file <!DOCTYPE html& ...

What is the method for centering text above and below an image in the vertical middle?

I'm struggling to create a vertical center-aligned image gallery on my website with "PREVIOUS" and "NEXT" links for easy navigation. Despite multiple attempts, I can't seem to get it right. Can someone guide me on how to achieve this effect? ...

Successful AJAX value transfer with no corresponding output

I have implemented a functionality where a user types in a textarea, presses tab, and then an Ajax call retrieves data to populate a select box based on the input from the textarea. <body> <textarea id='test' name="test"></te ...

Styling targeted to a particular div

Currently, I am attempting to showcase a docx file on my webpage by converting it to HTML and then displaying it. However, this method generates CSS with rules that end up impacting the entire document. For instance, it changes all links to blue which co ...

When the state inspection fails due to a missing object property, the function will not work as intended

I'm currently in the process of developing a weather app. The user will input either a zip code or a city name + state, triggering the $.getJSON function to gather data. One key feature I am working on involves checking if the user's input is va ...

How can I iterate through the contents of each directory in AJAX without encountering duplicates?

My webpage is currently displaying the output like this: FOLDER1 images/FOLDER1/folder1_img.jpg images/FOLDER2/folder2_img.jpg images/FOLDER3/folder3_img.jpg FOLDER2 images/FOLDER2/folder2_img.jpg images/FOLDER3/folder3_imgjpg FOLDER3 images/FOLDER3/fol ...

How to customize TextField error color in Material-UI using conditional logic in React

Currently, I am incorporating the React Material-UI library into my project and faced with a challenge of conditionally changing the error color of a TextField. My goal is to alter the color of the helper text, border, text, and required marker to yellow ...

jquery target descendants with specific names

In the provided HTML code snippet, there is a main div with the class name of cxfeeditem feeditem, and it contains multiple child elements with similar class names and structure. My query pertains to extracting values from specific children within all the ...

Creating equality in height among input file fields and other elements with Bootstrap 5

Utilizing a straightforward template to display a registration form with a file attachment field: /* facing an issue when adjusting the height for myself */ .mycustom { line-height: 2.75; z-index: 999; } <!-- <a href="/cdn-cgi/l/email-protecti ...

Incorporate a comma after the second or third digit to separate currency values in JavaScript

Is there a way to add commas to currency values at the 3rd or 2nd place depending on the value itself? The desired output format is: 1000 => 1000 10000 => 10,000 210000 => 2,10,000 2010000 => 20,10,000 12010000 => 1,20,10,000 I am currentl ...

How can you connect a jQuery UI sortable component to an array?

Is there a way to connect a jQuery UI sortable element with an array in order to assign an index to each sortable element within the array? I am looking for a method to automatically sort the array based on the movement of the sortable elements. Any sugg ...

Unable to open modals in Bootstrap using jQuery script: modal not popping up

My attempt to create functionality for two buttons and two modals - reserve a campsite (id="reserveButton" should open id="reserveModal") and Log in (id="loginButton" should open id="loginModal") is not working. I ha ...

Troubleshooting: Issue with passing variables from jQuery $.ajax to PHP

I am working on creating a 'show more' button using jQuery AJAX. The button has a loaded attribute like this: <button type="button" class="smbt btn center-block" data-loaded="3">Show More</button> In my JavaScript file, I have the f ...

Overflowing container due to text in Material UI Accordion heading

While using the Material UI accordion, I have encountered an issue within the AccordionSummary component where the title should be displayed. I have created a flex layout with two flex items inside. The first flex item contains the accordion title, while ...

AJAX request triggers only on the initial occasion

I am currently facing an issue with my AJAX call that is set to delete a record from a table generated by django-tables2. Each row in the table has a delete button marked with a class of delete_relationship_button and an id representing the primary key of ...

The Bootstrap CDN is malfunctioning and displaying errors in the console

I attempted to incorporate Bootstrap 4.5 using the CDN provided on their main website. However, after adding all the code lines to my project, I encountered numerous issues with my custom CSS and the Bootstrap carousel failing to function. Upon inspecting, ...

I am interested in utilizing the image.onload method within the $.when function

I am trying to dynamically load images inside a loop with specific requirements. In the code below, I want the alert to fire only when an image is being loaded, and then another alert to fire only after the first alert has fired. Here is my code: for (i ...

Issues have been reported with Angular 10's router and anchorScrolling feature when used within a div that is positioned absolutely and has overflow set

I feel like I may be doing something incorrectly, but I can't quite pinpoint the issue. Any help or pointers would be greatly appreciated. My current setup involves Angular 10 and I have activated anchorScrolling in the app-routing.module file. const ...