changing css colors

Discussion in 'Javascript' started by Sam Carleton, Oct 27, 2006.

  1. Sam Carleton

    Sam Carleton Guest

    How do I change the CSS colors via JavaScript DOM? Let me explain...

    I am working on a Windows application (in C#) that displays some HTML.
    In one place the HTML is a status window. What happens is the static
    HTML page is embedded into the application. The static page displayed
    and then the C# code gets a hold of the HTML DOM from the web browser
    and updates what pieces need to be updated.

    What I need to do now is change the colors of everything on the static
    page. At present there is an embedded CSS style in the HTML and all
    the colors are defined there. Using the DOM, via C# code, how do I
    change the colors of everything?

    1: Can I simply update the CSS and it will auto magically happen? If
    so, how does one update the CSS via the DOM?
    2: Do I need to go to each individual item and change the color there?
    3: Is there a better way to do this all the way around?

    Sam
     
    Sam Carleton, Oct 27, 2006
    #1
    1. Advertising

  2. Sam Carleton

    webEater Guest

    Sam Carleton wrote:
    > How do I change the CSS colors via JavaScript DOM? Let me explain...
    >
    > I am working on a Windows application (in C#) that displays some HTML.
    > In one place the HTML is a status window. What happens is the static
    > HTML page is embedded into the application. The static page displayed
    > and then the C# code gets a hold of the HTML DOM from the web browser
    > and updates what pieces need to be updated.
    >
    > What I need to do now is change the colors of everything on the static
    > page. At present there is an embedded CSS style in the HTML and all
    > the colors are defined there. Using the DOM, via C# code, how do I
    > change the colors of everything?
    >
    > 1: Can I simply update the CSS and it will auto magically happen? If
    > so, how does one update the CSS via the DOM?
    > 2: Do I need to go to each individual item and change the color there?
    > 3: Is there a better way to do this all the way around?
    >
    > Sam


    Hi Sam,

    you can't do this via C# but only via JavaScript because C# works on
    your server. JavaScript is made to dynamically change/manipulate your
    DOM tree.

    For exampleto change the font color of an element with id "myElement":

    document.getElementById('myElement').style.color = 'red';

    To change the color of all DIV element which have the className
    "whatever", you should use prototype (a JavaScript library that extends
    JS language):

    $$('div.whatever').each(function(element) {
    element.style.color = 'red';
    });

    ^ This works in all current browsers, there are other JS methods to
    change a node of your CSS definition (would be easier) but it's not
    compatible to all browsers.

    But there's a way to change the complete StyleSheet (CSS) file to make
    your page look change.

    I hope this wil help you.

    Andi
     
    webEater, Oct 27, 2006
    #2
    1. Advertising

  3. Sam Carleton

    Guest

    As your css is embedded into the page you can have it modified. It's
    something like /html/head/style. But it will be an unparsed text value
    which you will have to parse by yourself.

    It seems to me the better solution is to use classes. Like

    /* in your css */
    body.default * {
    color: red;
    }

    body.changed * {
    color: green;
    }
    .... than ...
    <body class="default">..

    In this case you will only have to change the class attribute of an
    appropriate element (body in this case).

    Sam Carleton wrote:
    > How do I change the CSS colors via JavaScript DOM? Let me explain...
    >
    > I am working on a Windows application (in C#) that displays some HTML.
    > In one place the HTML is a status window. What happens is the static
    > HTML page is embedded into the application. The static page displayed
    > and then the C# code gets a hold of the HTML DOM from the web browser
    > and updates what pieces need to be updated.
    >
    > What I need to do now is change the colors of everything on the static
    > page. At present there is an embedded CSS style in the HTML and all
    > the colors are defined there. Using the DOM, via C# code, how do I
    > change the colors of everything?
    >
    > 1: Can I simply update the CSS and it will auto magically happen? If
    > so, how does one update the CSS via the DOM?
    > 2: Do I need to go to each individual item and change the color there?
    > 3: Is there a better way to do this all the way around?
    >
    > Sam
     
    , Oct 27, 2006
    #3
  4. Sam Carleton

    ASM Guest

    Sam Carleton a écrit :
    > How do I change the CSS colors via JavaScript DOM? Let me explain...
    >
    > I am working on a Windows application (in C#) that displays some HTML.
    > In one place the HTML is a status window. What happens is the static
    > HTML page is embedded into the application. The static page displayed
    > and then the C# code gets a hold of the HTML DOM from the web browser
    > and updates what pieces need to be updated.
    >
    > What I need to do now is change the colors of everything on the static
    > page. At present there is an embedded CSS style in the HTML and all
    > the colors are defined there. Using the DOM, via C# code, how do I
    > change the colors of everything?
    >
    > 1: Can I simply update the CSS and it will auto magically happen? If
    > so, how does one update the CSS via the DOM?
    > 2: Do I need to go to each individual item and change the color there?
    > 3: Is there a better way to do this all the way around?


    I think best way is to have 2 titled alternate styles sheets embedded in
    your files.
    Then you'll just have to switch between both (or more).


    <link rel="alternate stylesheet" type="text/css"
    href="../../css/yellow.css" media="all" title="Yellow">
    <link rel="alternate stylesheet" type="text/css"
    href="../../css/blue.css" media="all" title="Blue">


    Here functions for several alternate styles sheets :

    function setActiveStyleSheet(title) {
    var i, a, main='';
    for(i=0; (a = document.getElementsByTagName("link")); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1) {
    if(a.getAttribute("title")) {
    a.disabled = true;
    if(a.getAttribute("title") == title)
    a.disabled = false;
    }
    }
    }
    }

    function getActiveStyleSheet() {
    var i, a, main = 'Defaut';
    for(i=0; (a = document.getElementsByTagName("link")); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
    && a.getAttribute("rel").indexOf("alt") != -1
    && a.getAttribute("title")
    && !a.disabled) {
    main = a.getAttribute("title");
    return main;
    }
    }
    return null;
    }
     
    ASM, Oct 27, 2006
    #4
  5. Sam Carleton

    clintonG Guest

    I recommend a book ISBN 0-321-43032-8 entitled "Javascript and Ajax for the
    web, sixth edition."

    The title is very misleading as the book has scant information about using
    Ajax. It is however excellent with regard to Javascript, e.g. DHTML. The
    entire book is basically several dozen well written and clearly explained
    snippets of Javascript for general page tasks. It should have been called
    something like "DHTML Snippets Simplified with the Last Chapter Ajax
    Overview"

    I found this book at the library and intend to buy it myself...

    <%= Clinton Gallagher
    NET csgallagher AT metromilwaukee.com
    URL http://clintongallagher.metromilwaukee.com/


    "Sam Carleton" <> wrote in message
    news:...
    > How do I change the CSS colors via JavaScript DOM? Let me explain...
    >
    > I am working on a Windows application (in C#) that displays some HTML.
    > In one place the HTML is a status window. What happens is the static
    > HTML page is embedded into the application. The static page displayed
    > and then the C# code gets a hold of the HTML DOM from the web browser
    > and updates what pieces need to be updated.
    >
    > What I need to do now is change the colors of everything on the static
    > page. At present there is an embedded CSS style in the HTML and all
    > the colors are defined there. Using the DOM, via C# code, how do I
    > change the colors of everything?
    >
    > 1: Can I simply update the CSS and it will auto magically happen? If
    > so, how does one update the CSS via the DOM?
    > 2: Do I need to go to each individual item and change the color there?
    > 3: Is there a better way to do this all the way around?
    >
    > Sam
    >
     
    clintonG, Oct 27, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Randy

    TextBox changing colors

    Randy, Oct 26, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    368
    =?Utf-8?B?Q3VydF9DIFtNVlBd?=
    Oct 26, 2005
  2. No spam
    Replies:
    0
    Views:
    539
    No spam
    Mar 4, 2005
  3. Anders Christiansen

    Changing colors

    Anders Christiansen, Mar 3, 2004, in forum: HTML
    Replies:
    8
    Views:
    494
    Ryan Stewart
    Mar 3, 2004
  4. AAaron123

    TreeNode colors come from anchor colors

    AAaron123, Aug 7, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    542
    darrel
    Aug 7, 2008
  5. ZelluX
    Replies:
    3
    Views:
    1,893
    Peter Otten
    Dec 1, 2008
Loading...

Share This Page