"JQuery code causing window width to display only when viewport is being resized, not upon initial page

I am currently facing an issue where the viewport width is not being displayed on page load. Does anyone know what might be causing this problem and how it can be resolved?

function onViewWidthChange() {
    var viewWidth = $(window).width();
    console.log(viewWidth);
    if (viewWidth > 776) {
        $("area[data-toggle]").click(function(e) {
            e.preventDefault();  // prevent navigating
            var selector = $(this).data("toggle");  // get corresponding element
            $(".detail-box").hide();
            $(".detail-box"+selector).show().addClass('animated fadeIn');
        });
    }
}

$(window).on('load, resize', onViewWidthChange);

Answer №1

Using a comma as a separator is not necessary. The elements can be separated by space and it will still work. For more information, please visit the following link.

$(window).on('load resize', mobileViewUpdate);

Answer №2

You might be invoking this function inside a ready handler, which means the window has already finished loading and the window load event has been triggered.

Therefore, adding a load event listener at that moment will never execute.

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

Assign values to the 'files' property of a Grunt task configuration using a custom function

Can the files property of a task configuration in Grunt be dynamically populated via a function? For example: concat: { angularSourceJs: { src: function() { return angularSort(grunt.file.expand('src/**/*.js')); ...

The issue of a property name not existing on a type in Typescript and Angular2

The answer provided for this particular question using the find method is not functioning in TypeScript, resulting in a compilation error. After reviewing similar inquiries, it appears that each one has its own unique context. Below is the array being ref ...

Transforming JSON data in Node JS according to the city

I currently have a JSON object that contains information about various products and their details in different cities. const data = [ { "city name": "Chennai", "product name": "Apple", ...

What is the best way to select an element with a dynamic ID in jQuery?

I'm encountering an issue when passing the ID through a directive. I'm unable to access the element using jQuery within the Link function, even though the element is receiving the correct dynamic ID as a parameter: Here's the Directive: (f ...

Disable other actions during jquery change event validation

Is there an answer for this question already? If so, I apologize! Here is my situation: I have an edit field that is validated when the .change() event occurs. What I am trying to achieve is this: if a user types something and then immediately clicks the s ...

ngBlur functions correctly only when focused element is tabbed out of

When an input field loses focus, I am trying to call a method using ngBlur. This seems to work well when removing focus by pressing the tab button. However, if focus is removed by clicking somewhere else in the form, the ngBlur does not behave as expected. ...

Transform every occurrence of http:// to https:// with the help of jQuery or Javascript

Looking for a way to convert links in src tags to use https://, specifically Youtube iframes. Here is an example: <iframe width='650' height='365' src="http://www.youtube.com/embed/y9kBixgYKzw" frameborder="0" allowfullscreen>< ...

Utilize import and export statements to transfer an HTML tag between two JavaScript files

I have two HTML files linked to two JS files. I want to save an HTML tag from HTML1 with JS1 in a Variable and export it. Then import it in the JS2 file and use it in HTML2 I have tried many ways but nothing seems to work, something as simple as this Exp ...

How to align two divs in CSS when they are not always both present

Currently working on CSS styling for a store. I have a div that aligns the buy button to the left, and Prev and View Next images to the right. The issue arises when the "buy" button is occasionally not present due to PHP functionality. When the buy butto ...

Using Django templating, you can incorporate other templates with the `

Check out my Directory Tree. The structure of the directory is as follows: {% load static %} <!DOCTYPE html> <html lang="en"> {% include 'weapons/head/head.html' %} {% include 'weapons/body/body.html' %} </html ...

The anonymous function in the Google strategy is not being executed

I am currently working on implementing Passport to allow users to log in to my website using their Google accounts. I am utilizing yarn along with the following relevant packages: [email protected], and passport-google-oauth20@^1.0.0. The issue I am f ...

Incorporate a fresh element into an object after its initial creation

Hello, I am looking to create an object in JavaScript that includes an array-object field called "Cities." Within each city entry, there should be information such as the city's name, ID, key, and a District array object containing town data for that ...

AngularJS directive doesn't refresh template when scope values are fetched dynamically through ajax requests

Attempting to give this question a precise title as possible, I find myself struggling with an issue in AngularJS. While trying to showcase my problem through a jsfiddle, it turned out to be too reliant on multiple files and not yet accessible online. So p ...

Combining arrays of JSON Objects and organizing them with Javascript

I have a JSON object containing arrays for different country regions. I am attempting to combine these arrays into a single select dropdown menu. The JSON structure is as follows: "latinamerica": [ "Argentina", "Bolivia", "Brazil", ...

Filter out specific fields from an object when populating in MongoDB using the aggregate method

Is there a way to use the populate() function in MongoDB to exclude specific fields like email and address, and only retrieve the name? For example: const results = await Seller.aggregate(aggregatePipeline).exec(); const sellers = await Seller.populate(re ...

What could be causing the Ioncol not triggering the Onclick event?

I am facing an issue where my onclick event is not working on an ion-col. The error message says that the method I call "is not defined at html element.onclick". Here is a snippet of my code: <ion-row style="width:100%; height:6%; border: 1px solid # ...

Using jQuery to access specific elements within the DOM by navigating through the document structure

I'm currently facing a challenge with using jquery's parents/siblings functions to locate specific input elements. Despite my efforts, I can't seem to get it right. Here is the HTML structure I am working with: <div id="ExtrasOptions"&g ...

Using ASP.NET MVC, pass a list of values separated by commas to an action method

Hey there, I'm facing an issue with an ajax call where I am trying to retrieve values from an html select multiple tag. The problem arises when I attempt to pass these values into my controller as I keep getting a null reference error in my controller ...

Error: The dynamic selection function is experiencing an issue where it is unable to read the "map" property of an undefined

Currently, I am in the process of creating a React component that includes the usage of a select HTML input. The implementation is defined as shown below: <select className="form-control-mt-3" id="clientList" name="clientList" onChange={this.handleC ...

Restrict the maximum character count within a TextArea using jQuery

Is there a way to calculate the total number of characters entered in a text area box? For example, if I have a textbox like this: <%: Html.TextArea("Message", "", new { @title = "Enter the Message" })%> I need to limit the input to 160 characters ...