JavaScript will not resize the Div element

Although it may seem silly, as this is my profession, I am struggling with some nuances of CSS and JavaScript positioning and sizing.

I have a function that animates objects by changing their properties using an easing function. However, each time the animation progresses, a callback to resize a test div is called but the div remains unchanged. It seems like a simple issue, but I can't figure it out.

Here is a link to a fiddle showcasing the problem: http://jsfiddle.net/sd9tJ/

Below is a snippet of the code for reference:

$(document).ready(function() {

    var surface = {w : 100, h : 100};

    var $div = $('#test');

    $t.animate(surface, {w : 200, h : 200}, 2000, $t.easing.linear, function() {
        document.body.innerHTML += '('+Math.round(surface.w)+'/'+Math.round(surface.h)+')';
        $div.css({
            width  : Math.round(surface.w) + 'px',
            height : Math.round(surface.h) + 'px'
        });
    });
});

The values are correct, but the style doesn't update, leading me to believe it's a CSS-related issue.

Any insights or solutions would be greatly appreciated. Thank you!

Answer №1

The issue arises from a particular debugging method:

    document.body.innerHTML += ... ;

This action captures the original HTML content of the <div> and reinserts it as it was when the page initially loaded. Removing this line resolves the problem.

Edit — Using $('#test') in the event handler works because it prompts jQuery to locate the element again, thus detecting the updated version and functioning properly.

The complication with adding to the document in this manner is equivalent to:

    document.body.innerHTML = document.body.innerHTML + whatever;

Retrieving the current innerHTML presents the page without any style modifications to the element. This then recreates the entire page DOM. It is advisable to utilize console.log(), or create a separate element on the page and update its HTML individually.

Answer №2

Convert it to:


    $('#target').css({
        width  : Math.round(area.width) + 'px',
        height : Math.round(area.height) + 'px'
    });

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

The process of determining array.reduce between two collections in MongoDb

I have two MongoDB collections with a one-to-one relationship. I want to perform an array.reduce operation based on both of these collections, like so: CollectionA: [ { _id: 1, valueA: 234, idB: 1 }, { _id: 2, valueA: 64, idB: 2 }, { _id: 3, valueA: ...

Encounter an issue while trying to fetch a JavaScript value from a variable using Selenium with Python

Looking to extract the value of the variable remainingTimeString from this specific webpage using Python and Selenium webdriver. I am attempting to utilize the driver.execute_script() function in my code snippet below: import selenium.webdriver options = ...

list of current items on the sidebar that are currently being

Hey everyone, I could use some assistance with customizing my sidebar design. I am trying to implement an active state in my sidebar using JavaScript. I attempted to utilize the element.css() method to apply styles when an element is clicked. However, I no ...

Rendering with Node.js Express and Mongoose after saving data

I recently started diving into Node.js Express and Mongoose while constructing a basic blog platform. My focus is on establishing routes for handling simple database tasks, but I'm feeling uncertain about managing asynchronous functions and ensuring ...

Utilizing $.getJSON to initiate a selection change event

I'm currently working on implementing a feature that involves adding categories to a dropdown list using jQuery Ajax. The goal is to load subcategories when a particular option is selected. However, I've encountered an issue where the addition o ...

Display an image in a pop-up when hovering over it briefly

When you run the code snippet and hover over "Hover here", a picture of grumpy cat will appear, but the image flashes on and off repeatedly: To see the image consistently, you may need to move your cursor away from "Hover here" and hover over it again. ...

Issues with splicing an Angular array using pure JavaScript

Could someone please help me figure out what I'm doing incorrectly? I have some JSON data that I am converting into an array for use in an ng-repeat. Before looping through the array, I am checking the event dates against today's date and removin ...

What is the best way to eliminate the unattractive white border surrounding dialog boxes?

Is there a way to remove the unsightly white outline around the UI Dialog? Check out this picture of the dialog I've tried various solutions I found on Google and Stack Overflow, such as: .ui-widget-content { border: none !important; outlin ...

Dealing with the occurrence of '00' in JSON output in JavaScript or Coldfusion

To get straight to the point, I'm having an issue with Jquery and ajax in updating a select box based on another select box. Everything seems to be working fine, except that the fail method is triggered instead of the done method when the JSON is retu ...

Tracking lengthy calculations in HTML using JavaScript is a breeze with this straightforward method

Need assistance with updating HTML page during a long loop process. var updateLog = document.getElementById("log"); for (count = 2; (count < 10000 && calculatedPower < power); count = count * 2) { calculatedPower = calculate_power(count, w, df, de ...

No changes were seen in CSS animation when using ng-repeat in AngularJS 1.3.1

Recently delving into the world of AngularJS and I seem to be missing something. I attempted a basic CSS animation [plunker][1] but it doesn't seem to be working. Any suggestions or assistance would be greatly appreciated. Thanks in advance! [1]: htt ...

Tips to position two shortcode elements next to each other horizontally

I have some code here and I am looking to align the two elements at the bottom, "php echo do_shortcode(wcas-search-form);" and "php do_action('hamzahshop_custom_min_cart');" on the same line horizontally. I am new to this and struggling to figure ...

How can I determine if any of the values in the array (both previous and current) are identical?

I am facing a challenge with 3 input fields that could potentially have the same values, but I need to ensure uniqueness for each field. {productFormData.Roles.map((role: string, index: number) => { return ( <div className={`form-group in ...

Ways to stop click function execution in jQuery

Here is the HTML code I am working with: <h4 class="milestonehead" style="cursor: pointer;" onclick="editFun('35');"> Some Header <img src="/images/delete.gif" style="float: right;" onclick="deletefun('35');"> < ...

Simple Steps for Making a Get Request using Vuex in Vue.js

I'm trying to figure out how to store products in Vuex within my index component. import Vue from 'vue' import Vuex from 'vuex' import cart from "./modules/cart"; import createPersistedState from "vuex-persistedstate ...

How do I send events from iOS (Swift) to JavaScript in React Native?

Currently, I am in the midst of a project where my goal is to seamlessly integrate React-Native into a native Swift application. To ensure that both sides are kept in sync with the state, I have implemented a 'message bus' - a mechanism designed ...

Executing a JavaScript code in a Python webdriver: A step-by-step guide

Using Selenium 2 Python webdriver: I encountered an issue where I needed to click on a hidden element due to a hover effect. In search of solutions to unhide and select the element, I came across the following examples: Example in Java: JavascriptExecut ...

Converting HTML Javascript to JAVA with the help of Selenium

Can someone help me extract the value of h1 as a string using selenium? Check out the HTML javascript snippet below- <script type="text/javascript"> $(window).load(function() { var $windowHeight = $(window).height() -12; $(" ...

Determine the width of the device screen and execute specific JavaScript files based on the screen size

I am working on two different pieces of JavaScript code. My goal is to first detect the width of the screen, and then dynamically choose which JS file to run based on this width. Can someone guide me on how to achieve this correctly? $(document).ready(f ...

React: automatically close other SubMenus when a new SubMenu is opened

Is there a way to automatically close all other open SubMenus when a user opens a new SubMenu? If anyone has a solution, I would greatly appreciate the help! This is my code: Menu.tsx -> const Menu: React.FC = ({ data }) => { return ( ...