When resizing the window, the width change in JQuery always resets to 0

I've been trying to adjust the width of a div element when the window is resized, but for some reason, the width always ends up being 0 after the resize event.

   function resizeUploadField() {
        var uploadInputField = $('.input-append');
        var uploadSelectButton = $('.btn-file');
        var uploadRemoveButton = $('.fileupload-exists');

        if (uploadRemoveButton.length) {

        }
        console.log(uploadInputField.width());
        uploadInputField.width(uploadInputField.width() - uploadSelectButton.outerWidth() - 2);
        console.log(uploadInputField.width());
    }


    $(document).ready(function() {
        var windowWidth = $(window).width();
        resizeUploadField();

        $(window).resize(function(event) {
            console.log($(event.target).width());
            if ($(event.target).width() != windowWidth) {
                resizeUploadField();
                windowWidth =  $(event.target).width();
            }

        });

It seems like the resizeUploadField function is working fine because the field resizes correctly when called in onDocumentReady.

If anyone can provide insight into what might be causing this issue, I would greatly appreciate it!

Answer №1

Consider redefining the windowWidth = $(window).width(); within the resize() function.

For example:

$(document).ready(function() {
    var windowWidth = $(window).width();
    resizeUploadField();

    $(window).resize(function(event) {
        windowWidth = $(window).width();
        console.log($(event.target).width());
        if ($(event.target).width() != windowWidth) {
            resizeUploadField();
            windowWidth = $(event.target).width();
        }
    });
});

I hope this clarifies things for you.

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

What are some ways I can adjust line-height without changing the height of the cursor?

When working in a contenteditable div, I am utilizing line-height to create some spacing between lines. The following example illustrates the issue: <div style="padding: 50px; width: 90px; line-height: 2em;" contenteditable="true"> line height lin ...

MongoDB has incorrect date and time records

I've been working on a blog site where entries are logged with the time and date they were posted and stored in MongoDB. Everything was working fine on my local machine, but when I deployed the site to Heroku, I noticed that the date displayed is 8 ho ...

Implementing a two-column infinite scrolling feature using ISCroll

I am looking to incorporate multi-column infinite scrolling using IScroll infinite scrolling. I want the content of my HTML to appear as follows: <ul> <li>A</li> <li>B</li> <li>C</li> <li>D</li> ...

Tips for detecting the height of a row with 2 columns in CSS

I am attempting to create two text/image boxes side by side, where one box dictates the height of both on desktop. In this scenario, the leftcol box should expand its background color to match the size of rightcol and center the content. On mobile devices, ...

Using JQuery to eliminate Javascript code after setting up an event listener, but prior to the listener being activated

Having trouble finding a solution to my question through search. I'm sorry if it has already been asked before. I am attempting to define an event listener and immediately remove the JS code after defining it. The challenge is that I want the removal ...

encountering a glitch during the electron.js build process with nextjs

When attempting to build Electron.js with Next.js, I keep encountering this persistent error. I have updated my packages and reinstalled node modules multiple times, but I am still unable to resolve it. C:\Users\Himanshu\Desktop\claros& ...

Are there any methods available to ensure that images maintain consistent quality on an HTML webpage without being stretched?

Within a card-group container, I am utilizing pre-built components from Bootstrap. The 'products' variable holds an array of JSON objects representing products for an online shopping store. <div class="card-group"> <% p ...

The most basic form of req.body will consistently be devoid of any content

I am currently working on passing basic data from the client to the server using a POST request for testing purposes. However, I am encountering issues with receiving empty or undefined logs on req.body. Server: //jshint esversion:6 const express = requi ...

Unexpected '->' found on page during loading

After migrating my WordPress site from localhost to the live server, I encountered an issue where "-->" appeared before the page finished loading. Can anyone explain this strange behavior? In addition to this issue, the jQuery scripts I had implemented ...

Uploading Multiple Files with Ajax and jQuery

I've been trying to implement an ajax multiple file upload form, but I'm facing issues with its functionality. Here is the HTML Code: <form method="post" id="postfrom" enctype="multipart/form-data"> <input type="file" name="file" i ...

"Trying to apply Regular Expressions for replacement is proving to be

The .replace function seems to be ineffective with these RegExp patterns. (((array[0].replace("%%_%", "<ol><li>")).replace("%_%%", "</li></ol>")).replace("%_%", "<li></li>")) Any assistance would be greatly appreciated ...

Building a remote shell using Node.js with the ability to interpret control sequences

Currently, I am working on developing a remote shell using Node.js. Here's the code that I have implemented so far : Client var net = require('net'); var client = net.connect({port: 1234}, function(){ process.stdin.pipe(client); clien ...

jQuery's ajax function fails to detect a 401 error

Having trouble with exception handling on the client side? Everything was going smoothly until I introduced HTTP401. Take a look at my base controller that overrides OnException: protected override void OnException(ExceptionContext filterContext) { va ...

Error: The term "Image" is unrecognizable

I'm in the process of creating a website where I want to display images via links. If the image is missing or only 1 pixel wide, I need the site to show an alternative image. My technology stack includes Jade/Pug and JS. Before rendering the links on ...

The second occurrence of a jQuery event

When a user left-clicks on the menu, it should load a view in a draggable box. However, the functionality is not working as expected. Sometimes you need to click twice - the first time the box appears but is not draggable, and the second time a new box app ...

Identifying the active input using React hooks

Currently exploring React Hooks and trying to determine which input is currently in focus. I have a collection of inputs that are generated dynamically, and I would like to identify the selected one so that I can append a value to it upon button click. ...

What is the best way to access my backend API on a web hosting platform?

To successfully send information from a contact form through an email, I need to access my backend API. My app is deployed on a webhost called Kinghost and I have two URLs provided: the first one is the generic mywebaddr.com:port-number, and the second one ...

What is the method to apply custom styles (CSS) to individual options within a jQuery UI selectmenu?

When working with an HTML form that includes a select element, I encountered a design challenge. <select id='myselect'> <option value='1'>1</option> <option value='2'>2</option> ... <option va ...

JavaScript function not being executed after AJAX response in HTML

There seems to be a problem with my jQuery code. After receiving an html response from an ajax call and prepending it to a div, a div within that received html is not triggering a function in my external javascript file. In the index.php file, I have incl ...

What could be causing an undefined error when running Javascript with Python and Selenium?

My goal is to utilize Javascript for retrieving a table body element on a webpage. Upon executing the script immediately, I receive an undefined response. However, if I introduce a few seconds delay, it functions correctly. def fetch_row_list(browser): ...