When the page is fully loaded, I am trying to utilize jQuery with functions like $(window).ready(), but unfortunately, it does not seem to be functioning as expected


Utilizing jquery to dynamically adjust the content on my html page based on the page width. Upon the page being fully loaded (using the

$(document).ready(function({\\there is some code here!}));
), I aim to execute certain functions.
My objective is to hide the main menu on smaller page widths (less than 835px) and replace it with something else. Page resizing plays a crucial role in achieving this. Below is the code snippet I am using for this purpose:

function deleteMainMenu(){
var width = $(window).width();
if(width <= 835){
$("#main-menu").empty();
$("#main-menu").append('add some html code here');
$("#logo").attr('class', 'col-xs-6');
$("#main-menu").attr('class', 'col-xs-6');

}
else{
$("#logo").attr('class', 'col-sm-2');
$("#main-menu").attr('class', 'col-sm-10');
$("#main-menu").empty();
$("#main-menu:empty").append('reset my old html code');
}
}

These functions are invoked with the following code:

$(document).ready(function(){           
    deleteMainMenu();
});

$( window ).resize(function(){
    deleteMainMenu();
});

Although the code works flawlessly when I adjust the page size, if I initially set the page width to less than 835px and then refresh the page, nothing happens!

Upon testing it on my phone, the changes did not take effect immediately. Instead, the functions were executed only after scrolling.

What steps should I take next? Thanks in advance!

Answer №1

Instead of using javascript, CSS offers some great solutions for this scenario.

Consider Bootstrap

Based on the classes you're using (col-xs-6), it seems like you're utilizing bootstrap. Bootstrap has some fantastic features, especially for situations like this. Take a look at this link for responsive utility options.

Non-bootstrap Approach

If you prefer not to use bootstrap utilities, you can try the following method:

Create two menus (.menu_big and .menu_small) and show/hide them using media queries.

.menu_big{
  display: none;
}
@media screen and (min-width: 835px) {
  .menu_small{
    display: none;
  }
  .menu_big{
    display: block;
  }
}

Check out the demo here

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

Issue with launching Android application from website

I am looking to add a feature to my Android app that will open the app when a specific link (for example, www.example.org) is visited in the web browser. I have been experimenting with intent filters, but haven't had much success so far. Although I h ...

Detecting click events on a JW Player using jQuery library on an iPad device

Currently, I have implemented jwplayer on a webpage. The issue I am facing is that when one of the navigation menus is clicked, it appears on top of the video. This functionality works perfectly on a desktop, even with the HTML5 player. However, on an iPad ...

Pattern matching using regex can also be used to restrict the number of characters allowed

Having some trouble with regex to match a specific pattern and also limit the number of characters: Let's say I have allowed number prefixes: 2, 31, 32, 35, 37, 38, 39, 41, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60 I only want numb ...

Pass JavaScript variables to a PHP file with the help of AJAX

Hey there, I'm new to developing web-based applications and currently learning about AJAX. I've run into a problem while trying to make an AJAX request with user inputs as variables and fetching the same variables in a PHP file. Below is the code ...

npm ERROR: Unable to install the package named "<packageName>" because it conflicts with an existing package of the same name

Currently, I am attempting to incorporate the jsonfile package into my project. However, I am encountering a couple of errors: An issue arises when attempting to install a package with the same name as another package within the same directory. (Despite ...

Creating a unique card component with React and custom Tailwind CSS styling

I am working on creating a project component using React: Despite my efforts, I have not been able to find the correct documentation for assistance. Additionally, I am facing challenges in utilizing next/image to ensure that it is the correct size. This ...

`Send data to a C# controller function by utilizing <button> and jquery`

My bootstrap modal contains multiple buttons for downloading files in different formats. I can successfully trigger the controller method by setting the onclick function like this: onclick="location.href='@Url.Action("DownloadAsJPG", "Home")'" ...

Using setInterval to perform an AJAX call and update a textarea may not function properly on Internet Explorer 8

I have developed a webpage that serves as a real-time log for monitoring a `txt` file continuously updated on Unix. To achieve this, I am utilizing Ajax requests to fetch data from a PHP script which reads the file and populates the content into a `textare ...

Post request in ExpressJS fails to redirect user after submission

My current project involves developing a web application using NodeJS and ExpressJS. One issue I am encountering is with the login functionality on my website. After filling out the login form, the user's credentials are sent via a post request to the ...

You cannot call this expression. The type 'String' does not have any call signatures. Error ts(2349)

Here is the User class I am working with: class User { private _email: string; public get email(): string { return this._email; } public set email(value: string) { this._email = value; } ...

No validation errors are currently being displayed. I am attempting to retrieve the error message when validation fails

My form validation error has suddenly stopped working. It was functioning properly yesterday, so I must have made a mistake somewhere, but I can't seem to pinpoint it. Now, when I attempt to sign up without entering any information, it just redirects ...

The WebView displays the source HTML using the loadDataWithBaseURL method instead of showing a rendered view

I am currently working on an application that utilizes WebView to display custom HTML content. However, I am facing an issue where when I execute the following code: loadDAtaWithBaseURL(URL, "<html><h1>TEST</h1></html>", "text/htm ...

CSS - Make the entire div clickable while leaving a specific area non-clickable

I have a block containing some information. Within this div is a hidden button labeled .remove. Clicking on the main block will take you to another page, while hovering over the div will reveal the button. However, clicking the button also navigates you aw ...

How come the express.static() function is failing to load my .js and .css files from the intended path?

const express = require('express'); const app = express(); const server = require('http').Server(app); const io = require('socket.io').listen(server); const path = require('path'); let lobbies = new Array(); app.us ...

PHP is sending incomplete email messages

After implementing a PHP script to send an email upon unsubscribing, I encountered an issue where only part of the email was being sent. Here is a JSFiddle example that represents how the complete email should look, although it may not appear neat due to b ...

Updating the initialState in your application while employing the useState function with React Hooks

I'm currently facing an issue with updating the image value in the local state. Everything works fine initially, but once I return to the same page, the initialState seems to retain the previous value. To resolve this, I find myself having to assign t ...

SyntaxError: VM3419:1 - Unexpected token "<" was not caught

I'm exploring AJAX techniques in a demo project where I retrieve data from the server using AJAX methods. However, I encountered the following error: " Uncaught SyntaxError: Unexpected token< xhr.onreadystatechange @main.js" JS: (function ...

What is the best way to prioritize List Items over menu items in the front?

Sorry if the title is unclear, let me clarify with some images. First, here is how the navigation bar looks in full window view: Now, this is what happens when I decrease the screen resolution: The items list seems to be hiding behind the menu items. Ho ...

Having trouble with Vue.js not returning HTML elements from methods properly?

I have been attempting to retrieve html elements from a method, and I thought of using v-html for this purpose (not sure if there is a better approach). However, I seem to have encountered an issue with backtick templates and string interpolation. An error ...

Creating a one-of-a-kind entry by adding a number in JavaScript

I am looking for a way to automatically add an incrementing number to filenames in my database if the filename already exists. For example, if I try to add a file with the name DOC and it is already present as DOC-1, then the new filename should be DOC-2. ...