View Single Post
Old May 23rd, 2006, 7:48 PM   #1
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,024
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
How To Change The Style/Class Of An Object?

How do I change the style/class of an object with javascript?

/* GET AN OBJECT */

var objects = new Array();
var type = 0;
if (document.getElementById)
{
    type = 1;
}
else if (document.all)
{
    type = 2;
}
else if (document.layers)
{
    type = 3;
}
function get_object(idname)
{
    if (typeof(objects[idname]) == "undefined")
    {
        if (type == 1)
        {
            objects[idname] = document.getElementById(idname);
        }
        else if (type == 2)
        {
            objects[idname] = document.all[idname];
        }
        else if (type == 3)
        {
            objects[idname] = document.layers[idname];
        }
    }
    return objects[idname];
}


Then I try to change the class/style like so:
x = get_object('some_id');
x.style = 'background-color: #000';

I get the error: "setting a property that has only a getter"

And if I try class instead of style, it doesn't like me using the word class in the javascript.
Sane is offline   Reply With Quote