Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 234525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:11:30+00:00 2026-05-11T20:11:30+00:00

I want to make a CSS style switcher in JavaScript, same as Digg does

  • 0

I want to make a CSS style switcher in JavaScript, same as Digg does here

image from web archive, see summary for more details

I am having a div, in which I want to change the style of the div box on the basis of theme selection.

I don’t want to use jQuery, I want to develop this code in pure JavaScript.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:11:30+00:00Added an answer on May 11, 2026 at 8:11 pm

    Inspecting the source code and how it changes as you change themes, it is really quite simple. Digg is harnessing the awesomeness of CSS to drastically change content by simply changing the class of an element. All you have to do is this:

    <div id="theme_viewer">
    <!-- HTML code for your widget/thing to be styled -->
    </div>
    

    And then you can have your themes:

    <ul>
      <li><a href="#" onclick="return switchTheme('theme1');">theme 1</a></li>
      <li><a href="#" onclick="return switchTheme('theme2');">theme 2</a></li>
      <li><a href="#" onclick="return switchTheme('theme3');">theme 3</a></li>
      <li><a href="#" onclick="return switchTheme('theme4');">theme 4</a></li>
    </ul>
    

    All switchTheme needs to then do is change the class of theme_viewer to the class passed:

    function switchTheme(theclass) {
        var viewer = document.getElementById('theme_viewer');
        viewer.className = theclass;
        return false;
    }
    

    Your CSS stylesheets can then do whatever the different styles call for:

    #theme_viewer.theme1 {
        background-color: red;
    }
    
    #theme_viewer.theme2 {
        background-color: blue;
    }
    
    #theme_viewer.theme3 {
        background-color: black;
        color: white;
    }
    
    #theme_viewer.theme4 {
        background-color: orange;
    }
    

    here is an example of this at work

    As mentioned in the comments by J-P and in edeverett’s answer, it is not a good idea to have your events inline as I have them in this example (ie, with the onclick attribute). I did it for simplicity, but it’s not a good idea. The better idea is to wait for your document to load and attach the event handlers dynamically with Javascript. This is known as unobtrusive Javascript, and it is a Good ThingTM.

    An example of the above example following good practices would look like this:

    <ul id='theme_options'>
      <li><a href="#" id='theme1'>theme 1</a></li>
      <li><a href="#" id='theme2'>theme 2</a></li>
      <li><a href="#" id='theme3'>theme 3</a></li>
      <li><a href="#" id='theme4'>theme 4</a></li>
    </ul>
    

    And then we use this cross browser addEvent function (written by John Resig, author of jQuery):

    function addEvent( obj, type, fn ) {
      if ( obj.attachEvent ) {
        obj['e'+type+fn] = fn;
        obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
        obj.attachEvent( 'on'+type, obj[type+fn] );
      } else
        obj.addEventListener( type, fn, false );
    }
    

    To add the event handlers:

    addEvent(window, 'load', function() {
        var opts = document.getElementById('theme_options');
        var links = opts.getElementsByTagName('a');
        for(var x = 0; x < links.length; x++) {
            addEvent(links[x], 'click', function() {
                switchTheme(links[x].id);
            });
        }
    });
    

    It might be a little more verbose than inline Javascript, but it is worth it. I know you said you’d like to avoid jQuery, but something as simple as this makes you start appreciating the library, as you could do it with jQuery with something as simple as this:

    $(function() {
        $('a','#theme_options').click(function() {
            switchTheme($(this).attr('id'));
            return false;
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 174k
  • Answers 174k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'm pretty sure that's what the NSError class is there… May 12, 2026 at 2:58 pm
  • Editorial Team
    Editorial Team added an answer How about a simple pre-Build action that copies your default… May 12, 2026 at 2:58 pm
  • Editorial Team
    Editorial Team added an answer JavaScript closures use the parent function scope, so PK[mode] will… May 12, 2026 at 2:58 pm

Related Questions

I am using a rich html editor and I want to make a whitelist
I'm using the jQuery datepicker to select dates. It works fine, except there is
Is hiding a <br /> tag with CSS a well defined way to make
I have a table that's generated by a normal PHP loop. What I want

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.