Jump to content

getStyle and setstyle


alejandro52

Recommended Posts

// Get a style property (name) of a specific element (elem)
function getStyle( elem, name ) {
// If the property exists in style[], then it's been set
// recently (and is current)
if (elem.style[name])
return elem.style[name];
// Otherwise, try to use IE's method
else if (elem.currentStyle)
return elem.currentStyle[name];
// Or the W3C's method, if it exists
else if (document.defaultView && document.defaultView.getComputedStyle) {
// It uses the traditional 'text-align' style of rule writing,
// instead of textAlign
name = name.replace(/([A-Z])/g,"-$1");
name = name.toLowerCase();
// Get the style object and get the value of the property (if it exists)
var s = document.defaultView.getComputedStyle(elem,"");
return s && s.getPropertyValue(name);
// Otherwise, we're using some other browser
} else
return null;
}

 

This is some code to get the style of an element independent of the browser .How can i change it to set the style of an element. I think that it would be good to make some sticky topics on functions that are widely in use for bypass differences in browsers or sites that already have some of them.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.