Wrox Programmer Forums
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 18th, 2004, 05:57 AM
Authorized User
 
Join Date: Jul 2003
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default Changing StyleSheets

Hello Everyone and thanks for your help in advance. I am developing a DHTML page with various rollover effects. I know that styles can be changed for an element via javascript. But I wanted to know if the CSS class for an element could be changed instead of changing each attribute separately, i.e. style.background.color? It would seem that this would be an easier way if possible, to change attributes rather than hardcoding it. Any feedback on this would be greatly appreciated. Thanks.

 
Old May 18th, 2004, 07:06 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Hugh,

Long time no see ;)

You can use the className property to change the class info for an entire object. The following example shows hows this works (it's really simple; just set myObject.className = 'NewClassName';):
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Change CSS Class</title>
    <style type="text/css">
    .ClassOut
    {
        background-color: black;
        color: white;
        font-weight: bold;
    }
    .ClassOver
    {
        background-color: white;
        color: black;
        font-weight: bold;
        border: 1px solid black;
    }
    </style>
</head>
<body>

<p class="ClassOut" onMouseOver="this.className='ClassOver';" 
        onMouseOut="this.className='ClassOut';">
Hi, I am a paragraph with text. If you hover your mouse 
    over me, my color changes . This is done by setting the 
<a href="http://msdn.microsoft.com/library/default.asp?
    url=/workshop/author/dhtml/reference/dhtml_reference_entry.asp?frame=true">
        className</a> CSS property.
</p>

</body>
</html>
Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old May 18th, 2004, 03:40 PM
Authorized User
 
Join Date: Jul 2003
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the help. Worked like a charm.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing stylesheets in FF & IE6 hericles Javascript How-To 0 August 15th, 2007 05:42 AM
passing parameters to xsl stylesheets smys123 Java Basics 2 June 22nd, 2007 03:42 PM
changing ActiveCell sedolphi Excel VBA 1 May 17th, 2004 02:37 PM
CSS Stylesheets Lucy Dreamweaver (all versions) 4 November 4th, 2003 04:14 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.