views:

188

answers:

5

according to the book that I am reading, it is better to change a css, by class when you are using javascript for changing css. but how? can someone give a sample snippet for this ? thanks in advance

+1  A: 

use the className property:

document.getElementById('your_element_s_id').className = 'cssClass';
knittl
Be carefull, if you just want to add a new class, this way will replace all the class="" attributes, not just append the new one.
Boris Guéry
+2  A: 

document.getElementById("my").className = 'myclass';

Adir
+9  A: 

Suppose you have:

<div id="mydiv" class="oldclass">text</div>

and the following styles:

.oldclass { color: blue }
.newclass { background-color: yellow }

You can change the class on mydiv in javascript like this:

document.getElementById('mydiv').className = 'newclass';

After the DOM manipulation you will be left with:

<div id="mydiv" class="newclass">text</div>

If you want to add a new css class without removing the old one, you can append to it:

document.getElementById('mydiv').className += ' newClass';

This will result in:

<div id="mydiv" class="oldclass newclass">text</div>
Asaph
A: 

You may also be interested in modifying it using jQuery: http://api.jquery.com/category/css/

Juri
+6  A: 

I'd highly recommend jQuery. It then becomes as simple as:

$('#mydiv').addClass('newclass');

You don't have to worry about removing the old class then as addClass() will only append to it. You also have removeClass();

The other advantage over the getElementById() method is you can apply it to multiple elements at the same time with a single line of code.

$('div').addClass('newclass');
$('.oldclass').addClass('newclass');

The first example will add the class to all DIV elements on the page. The second example will add the new class to all elements that currently have the old class.

Andy Shellam
well, jQuery/mootools/$randomframework is always an option …
knittl
thanks for the tips sir. I'll get into that soon. I am just starting to read about javascript ;)
sasori
Another benefit in using a framework is that you can more easily add or remove class names rather than simply replace the entire class attribute's value (e.g. to remove "myclass" from an element with a class attribute of "myclass myotherclass" you'd otherwise have to replace the attribute value with "myotherclass" or do something funky like split the string into an array and remove the entry "myclass" from it before joining it back together, etc. Go Frameworks!
Alan
I never entertained the idea of using a Javascript library as I thought it'd just be yet another framework to use, another JS file to download, another place to debug to find problems... yada yada yada.One day I was bored, bit the bullet and tried jQuery. Within an hour I'd vowed never to create a website or webapp without it - it's so easy!You could probably even learn jQuery and never touch any other Javascript.
Andy Shellam
It's good to know how to do things without a framework, for understanding and when you want to make something truly lightweight, but there's no reason to not use a library like jQuery for most development.
Mark Snidovich
Totally agree, Mark. I now think of it like being a manager - never delegate work to a sub-ordinate that you wouldn't be able to do yourself. I.e. never use a framework to do something you wouldn't know how to do yourself.
Andy Shellam