views:

71

answers:

5

Hi!

I'm writing a jquery-plugin, that changes a css-value of certain elements on certain user-actions. On other actions the css-value should be reseted to their initial value.

As I found no way to get the initial css-values back, I just created an array that stores all initial values in the beginning.

I did this with:

var initialCSSValue = new Array()

quite in the beginning of my plugin and later, in some kind of setup-loop where all my elements get accessed I used

initialCSSValue[$(this)] = parseInt($(this).css('<CSS-attribute>'));

This works very fine in Firefox. However, I just found out, that IE (even v8) has problems with accessing the certain value again using

initialCSSValue[$(this)]

somewhere else in the code. I think this is due to the fact, that I use an object ($(this)) as a variable-name.

Is there a way arround this problem?

Thank you

+1  A: 

Indexing an array with a jQuery object seems fishy. I'd use the ID of the object to key the array.

initialCSSValue[$(this).attr("id")] = parseInt...
Stefan Kendall
This would only work if you are assuming everything has an `id`, which is a pretty large assumption.
Doug Neiner
Thank you! Using the ID seems not that advantegeous in this case, as it could be possible, that certain elements appear twice on a web-page. For example if i use my plugin for styling a search-input, there could be a search input in the sidebar of a page and on the main part of a search page generated by the same search-script. In this case I would need to use the same ID twice - impossible.
Marcel
IDs should always be unique. You could key on another attribute, but IDs should be easiest.
Stefan Kendall
A: 

What about using

initialCSSValue[$(this).attr('id')];

This would assume you are correctly using each id only once.

sberry2A
Thank yo!Using the ID seems not that advantegeous in this case, as it could be possible, that certain elements appear twice on a web-page.For example if i use my plugin for styling a search-input, there could be a search input in the sidebar of a page and on the main part of a search page generated by the same search-script. In this case I would need to use the same ID twice - impossible.
Marcel
+2  A: 

Use $(this).data()

At first I was going to suggest using a combination of the ID and the attribute name, but every object might not have an ID. Instead, use the jQuery Data functions to attach the information directly to the element for easy, unique, access.

Do something like this (Where <CSS-attribute> is replaced with the css attribute name):

$(this).data('initial-<CSS-attribute>', parseInt( $(this).css('<CSS-attribute>') ) );

Then you can access it again like this:

$(this).data('initial-<CSS-attribute>');

Alternate way using data:

In your plugin, you could make a little helper function like this, if you wanted to avoid too much data usage:

var saveCSS = function (el, css_attribute ) {
   var data = $(el).data('initial-css');
   if(!data) data = {};
   data[css_attribute] = $(el).css(css_attribute);
   $(el).data('initial-css', data);
}
var readCSS = function (el, css_attribute) {
   var data = $(el).data('initial-css');
   if(data && data[css_attribute])
     return data[css_attribute];
   else
     return "";
}
Doug Neiner
Seems like a brilliant idea!I didn't know the Data functions yet (I'm quite new to jquery and javascript) but I will start reading about it right now.Thank you
Marcel
wow, works like a charm and hits my intention even better than my first (sluggish) solution.
Marcel
@Marcel Awesome! Be sure to mark this answer as accepted then. Good luck with your project and welcome to Stack Overflow!
Doug Neiner
A: 

Oh please, don't do that... :)

Write some CSS and use the addClass and removeClass - it leaves the styles untouched afterwards.

naugtur
I thought about this too and in fact I was willing to do this, if nobody had a better idea here. but now I'm really happy with the Doug Neiners ".data-solution", at it does exactly what it is intended to do. Thank you anyway!
Marcel
A: 

if anybody wants to see the plugin in action, see it here: http://www.sj-wien.at/leopoldstadt/zeug/marcel/slidlabel/jsproblem.html

marcel