$style - Get Any CSS Property Value of an Object

Posted by: the telerik blogs, on 01 Sep 2008 | View original | Bookmarked: 0 time(s)

Here is a crossbrowser JavaScript function for getting CSS property values from DOM elements. The function $style requires the specification of two arguments:

$style(ElementId, CssProperty);

ElementId - the ID of the element from which we will extract CSS property values;
CssProperty - the CSS property we will extract from the element, for example - "background-color";

The function $style:

function $style(ElementId, CssProperty)
{
function $(stringId)
{
return document.getElementById(stringId);
}
if($(ElementId).currentStyle)
{
var convertToCamelCase = CssProperty.replace(/\-(.)/g, function(m, l){return l.toUpperCase()});
return $(ElementId).currentStyle[convertToCamelCase];
}
else if (window.getComputedStyle)
{
var elementStyle = window.getComputedStyle($(ElementId), ");
return elementStyle.getPropertyValue(CssProperty);
}
}

Example usage of the function:

1. Register the runtime routine of the $style in the <head>.</head>of your webpage, or as an external script.

2. The CSS for the example:

<style type="text/css">
#Div1
{
border: solid 1px red;
background: #00ff00;
color: #000;
font-family: "Trebuchet MS", Arial, Verdana;
}
</style>

2. The HTML for the example:

<div id="Div1?>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras purus pede, aliquam vel, eleifend in, mollis sit amet, ante.</div>

3. The JavaScript for the example:

<script type="text/javascript">
function getElementStyles()
{
var elBgColor = $style("Div1?, "background-color");
var elForeColor = $style("Div1?, "color");
var elFont = $style("Div1?, "font-family");

alert('"Div1? style settings:\n\nbackground color: ' + elBgColor + ';\n' + 'color: ' + elForeColor + ';\n' + 'font-family: ' + elFont + ';');
}
</script>

4. And finally, you need an event to fire function. It could be any DOM event. For this example I have used the click event of a button:

<input type="button" onclick="getElementStyles()" value="Get Element Styles" />

Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: Ajax | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 908 | Hits: 8

Similar Posts

  • Introducing Recurring Appointments for Web.UI Scheduler ASP.NET AJAX more
  • Data-binding Telerik CoverFlow for Silverlight + some Routed Commands goodness more
  • Using T4 Templates for Simple DTOs more
  • Dynamic in C# 4.0: Creating Wrappers with DynamicObject more
  • Telerik Announced Official Version of Visual Style Builder for AJAX Controls more
  • querySelectorAll on old IE versions: something that doesnt work more
  • Multi-Monitor Support (VS 2010 and .NET 4 Series) more
  • Chat room questions from the EF Tips & Tricks webcast more
  • Creating Objects with Observable Properties in JavaScript more
  • A total n00bs guide to migrating from a custom data layer to Nhibernate: so many choices more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD