Home > JavaScript > JavaScript: change CSS

JavaScript: change CSS

This is a simple script that will change the CSS of any object.

You send the function the ID, the CSS attribute you want to change, and the value..

I actually struggled a bit with this as using dot syntax for the CSS attribute would not work. I had to use brackets.

<div id=”myDiv” onclick=”CSS_change(‘myDiv’,'color’,'#ff0000′)”>Change Color</div>

function CSS_change(ID, CSSATTRIB, VALUE)
{
    var obj_style = document.getElementById(ID).style;
    obj_style[CSSATTRIB] = VALUE;
}

Categories: JavaScript
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.