Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7494149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:20:57+00:00 2026-05-29T17:20:57+00:00

Sorry couldn’t think of a better title. :) Here goes, I have been pulling

  • 0

Sorry couldn’t think of a better title. 🙂

Here goes,

I have been pulling my hair out for hours trying to figure this out.

I have the following function:

function changeCss(elem, styles) {

   var oldstyles = styles;

   elem.css( styles )

   for(var key in styles){
       var oldstyle = input.css(key);
     oldstyles[key] = oldstyle;              //this line is the issue
       elem.data( 'oldstyle', oldstyles )                      
   }
}

Here is an example of how it gets called:

var theStyle = { border: '1px solid red'};
//elem is a jQuery object of an HTML element

changeCss(elem, thestyle);

The problem is that after the commented line ( line 8 ), styles‘s value is whatever oldstyles‘s value is.

So for example if that particular element’s CSS attribute for border was not set, ie undefined, styles‘s value will be { border: "" }

How is that even possible? That is why I am delcaring oldstyles; I’m making a “backup” of the styles variable.

I have changed the 3rd line with var oldstyles = {}; and it seems to work correctly.

But this isn’t how it is supposed to be, I need to have the old styles because later I want to reset the styles – but only the ones I changed.

I can’t understand why it “breaks” when oldstyles equals styles?

I hope I am giving enough code for you to understand, and also that I am expaining myself good enough. 🙂

If anything is unclear, please ask.

Thank you for any insight and help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T17:20:59+00:00Added an answer on May 29, 2026 at 5:20 pm

    It’s because oldstyles is actually just a reference to styles rather than a copy. As it appears that you are using jQuery you can use the jQuery.extend to make oldstyles a copy of styles:

    var oldstyles = {};
    jQuery.extend(oldstyles, styles);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for dodgy title, couldn't think of anything better... Anyway, I have written a
Sorry I couldn't think of a better title... So I have this database where
Sorry couldn't think of a better title. So here is an example of my
Sorry about the title, I couldn't think of a better way to describe the
Sorry for the title, I couldn't argue something better. Here is my question: Is
Sorry I couldn't come up with a better title. Here is my htaccess RewriteEngine
Sorry for the bad title, but I couldn't think of a better one. I'm
Sorry I couldn't think of a better way to title this. In Amazon's SimpleDB,
I know the title is vague, sorry I couldn't think of a better way
Sorry I couldn't think of a more descriptive title: We have an issue with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.