The animation to alter the width of a div using jQuery is not functioning correctly

Hey everyone, I'm facing an issue with my variable (widthPercent) where I store a percentage, for example: 67.33%.

When I try to change the width using jQuery animation, it doesn't work:

$(this).animate({
        width: widthPercent,
    }, 2500);
});

However, changing the width with CSS works perfectly fine:

$(this).css('width', widthPercent);

Does anyone have any ideas on what might be causing this problem?

Answer №1

Perhaps consider including the widthPercent in quotation marks.

This solution successfully resolved the issue for me.

$(document).ready(
                function(){
                    var widthPercent = "35%";
                    $("#btn").click(
                    function(){

                        $(this).animate({
                            width: widthPercent,
                        }, 2500);
                    });
                }
            );

The root of the problem may lie within how you've defined the widthPercent variable.

Answer №3

In order to properly display the width value, you must include a '%' character after it. Follow this example:

$(this).css('width', widthPercent + '%')

Check out this helpful jsfiddle link for a visual demonstration.

Answer №4

Here is an example of what you may be looking for:

Using jQuery:

$('#foo').click(function(){
    var widthPercent = '66%';
    $(this).animate({
        'width': widthPercent,
    }, 2500);
});

And the corresponding CSS:

#foo{
   width:300px;
   background:#ff0000;
   height:100px;   
}

You can also view this example on JSFiddle.

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 button is effectively disabled for a few seconds as needed, but unfortunately, the form cannot be submitted again using that button

The button is disabled for a specific duration as required, but the form does not get submitted through that button again. Below is the script code written in the head tag $(document).ready(function () { $('.myform').on('submit', ...

Generating table rows dynamically using functions

I am working on a table where I need to dynamically add or remove rows. Each row contains a hyperlink in the last column to delete the record. Sometimes, if the record is not found in the database, this can cause issues as new rows are added dynamically af ...

What is the best way to reset JQuery autocomplete in a newly loaded DIV?

I am facing an issue with an autocomplete widget inside a DIV, which was copied from the JQuery samples available at http://jqueryui.com/resources/demos/autocomplete/default.html Upon button presses, the content of the DIV gets replaced with new HTML. The ...

Unable to set focus on a disabled button programmatically, issue persists

$(function () { $('#save').click(function() { $('#save').attr('disabled', 'disabled'); console.log($(document.activeElement).attr('id')); }); $('#test').focus(); co ...

Leveraging MUI v5 sx Prop for Applying Various CSS Properties Simultaneously (such as margin, border-radius, and more)

Is there a more efficient way to write the following code that utilizes the sx prop's access to spacing units? <MUIComponent sx={{ borderRadius: '4px 8px 12px 16px' }} /> Could it be written like this instead? <MUIComponent sx={{ b ...

Trouble with implementing web methods through AJAX communication channel

my .cs code is auth_reg.aspx.cs (breakpoint shows the method is never reached) [WebMethod] public void ImageButton1_Click() { string strScript = "<script language='JavaScript'>alert('working')</script>"; Page.Regis ...

The absolute positioning is causing the <UL> <LI> elements to malfunction

Hello I have encountered an issue with the bootstrap menu that I am using. The first link is not functioning properly due to the position: absolute; attribute of the <li> element. Any suggestions on how to resolve this would be greatly appreciated. I ...

Error message: The Slick Carousal encountered an unexpected problem - TypeError:undefined is not a function

I'm having an issue with a script for a Slick Carousel inside of some Ajax Tabs. I keep encountering the Uncaught TypeError: undefined is not a function error, but I'm unsure what exactly it's pointing to. $(document).ready(function(){ ...

Tips for positioning a button directly under a horizontal navigation bar

How can I center a button just below a horizontal navigation menu, without overlapping the menu items and ensuring proper spacing when the menu expands? <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> < ...

Label embedded within field giving it a crispy touch

Utilizing Bootstrap5, Django, and Crispy Forms to develop a custom calculator application. The app is functioning as intended, but I am looking to modify the appearance of the forms. I've successfully eliminated the required field asterisk by includi ...

Initiate the python script on the client's end

Currently, I am in the process of initiating a Python script designed to parse a CSV file that has been uploaded by the user through the UI. On the client side, how can I effectively trigger the Python script (I have explored using AJAX HTTP requests)? Add ...

What is the best way to choose dynamic content?

This might seem like a trivial question, but it's definitely not (well, at least not to me). I understand that when attempting to attach an event to dynamic content, the .on() method must be used. However, I am faced with the challenge of selecting e ...

Avoid altering the Summernote HTML WYSIWYG editor page due to content CSS

We are currently utilizing the Summernote wysiwyg editor to review and preview html content, some of which contains external stylesheet references. This css not only affects Summernote itself but also alters the styling of the parent page where Summernote ...

What is the proper way to invoke the function located within a jQuery plugin?

I have incorporated a jquery plugin called vTicker on my webpage for automatic vertical news scrolling. This plugin, available from this link, works seamlessly with an rss jquery plugin. The integration is successful, but I now have a requirement to add a ...

Tips for capturing changes in a "count" variable and executing actions based on its value

I have a counter on my webpage and I'm trying to change the style of an element based on the count variable. I tried using the .change action but I haven't been able to get it working. It might not be the right solution. Can anyone provide some ...

Issue with Javascript form validation causing the form to still submit despite returning false

I am struggling to validate a form using JavaScript. The function is being called correctly and the alert is shown, but even after clicking ok on the alert, the form is still submitted. The function never returns false. I have come across this issue before ...

Can we incorporate various CSS libraries for individual components on our React site?

Let's say, I want to use different CSS libraries for each of my components - Home, About, Contact. Would it be feasible to utilize material ui for Home, semantic ui for About, and bootstrap for Contact? If so, what is the process for incorporating t ...

Sending a list via Ajax in Django

I recently executed an Ajax call successfully: $.ajax({ url:'/quespaper/intermediate_table', type:'GET', processData: false, data:{'url_link':url_link_copy,'updated ...

Using jQuery to find the ID of a div element within another div

I am currently utilizing jQuery UI sortable functionality to rearrange the list elements on my webpage. However, after sorting the elements, I am interested in obtaining the IDs of the buttons. At the moment, I am only able to retrieve the <div> elem ...

Is there a way to update the color of a button once the correct answer is clicked? I'm specifically looking to implement this feature in PHP using CodeIgniter

Within my interface, I have multiple rows containing unique buttons. Upon clicking a button, the system verifies if it corresponds to the correct answer in that specific row. The functionality of validating the responses is already functional. However, I a ...