Friday, January 18, 2008

Next! - getStyle

Hi, we all need sometimes to get the style of one HTMLElement, and most times we do


If there was a "color" rule on the element own style, that will work. But it wont work if the "color" was set on a CSS rule.
So that breaks, for example in PrototypeJS they have $("elid").visible() that should return true if the element is visible, how they check?!="none";

But that will say the object is visible if i set it to display:none on a CSS rule

While working with PrototypeJS i had lots of troubles cause i always had to use style="display:none" or the animation stuff ( ) would not work.

This guy did a fix on the code he use for animate CSS properties, so i adapted it to work on Next!, its cross browser

* Get the value of the style
* Based on
* @param {String} style
HTMLElement.prototype.getStyle=function(property, def){
if (property=="float" || property=="cssFloat" || property=="propertyFloat"){
if (property=="opacity" && this.filters){
return this.filters.item("Alpha").opacity/100;
return 1;
var style;
if(document.defaultView && document.defaultView.getComputedStyle){
style = document.defaultView.getComputedStyle(this, "").getPropertyValue(property);
if (style) {
return style;
property = property.camelize();
style = this.currentStyle[property];
return style ||[property];

This function itself fix also the "opacity" so in this lib you can not only set the opacity but you can get the opacity of the element even if it is a filter ( IE ) on some CSS rule

This code is need and really moves the library to a next stage, we can get the REAL style of the element not only the in-line style, just great!

Now that we have a getStyle we must also have a setstyle
setStyle its simpler it will just set the inline style of the element

* Set the style of the element
* @param {String|Object} style
* @return {HTMLElement}
if ({";"+style;
if (style.contains("opacity:")){
}else if ({
for (i in style){
if (i == "float" || i == "cssFloat" || i == "styleFloat") {[i];[i];
}else if (i=="opacity"){
return this;

This code is still missing a fix, it will be done soon, but the base idea is that you can do

More things are comming!!

No comments: