[Javascript] dynamically reading/changing values in a css class from javascript

Mike Dougherty mdougherty at pbp.com
Thu Jun 14 07:25:31 CDT 2007


On 6/14/07, Lee Bettridge <lbettridge at twowaytv.co.uk> wrote:
> Is it possible to read a value from a CSS class and assign it to a
> javascript variable ? and then in turn change the value in the CSS class
> from javascript?

I would recommend not even trying to change the CSS.  If you want to
read/set a style property via javascript, use obj.style.property.  If
you want to change a number of properties, I suggest changing the
object's className so a different CSS selector applies those styles:

var np = document.getElementById("nav");
np.className +=  np.className.replace(/navPanelTall/,"") + " " + "navPanelTall";
(the replace will remove an already existing class before adding it)

.navPanel
{
       position: absolute;
       background: url(gfx/panel.png) no-repeat;
       top: 325px;
       left: 39px;
       width: 561px;
       height: 124px;
       z-index: 2;
       visibility: hidden;
}

.navPanelTall { height: 250px; }



More information about the Javascript mailing list