1

I've a HTML markup and I've several CSS files with different properties assigned for the HTML elements which will act as the theme of the markup. Say the Markup is something like

<div id=main>
    <div>{More HTML elements, divs, spans etc}</div>
    <div>{More HTML elements, divs, spans etc}</div>
</div>

And the css is like

/*first.css*/
#main{
    someProperty: values;
}

#main div, #main ul li {
    someProperty: values;
}

Another css file

/*second.css*/
#main{
    someProperty: some other values;
}

#main div, #main ul li {
    someProperty: some other values;
}

There are more css files with different properties and values.

What I want to do is, at the click of a button, I want to change the CSS of the markup without changing the markup. So it will be something like

$('.btn').click(function(){
    var file = $(this).attr('id');
    //remove the previous css file
    //add the new css file (filename is the js var file+'.css')
});

I cannot keep doing $('#someId').css({ /* properties */}); as there are many properties in the CSS file.

So how do I best handle this??

3 Answers 3

2

I would say you can have single css with different properties you want to have in different classes. Whenever you want to change something. Just remove the current class using .removeClass() and add the new class using .addClass().

In case you want to switch the css files . Have a look here

Sign up to request clarification or add additional context in comments.

Comments

1

You can easily remove link elements from the head with jQuery, they are treated as any other HTML element.

You can remove a certain CSS file from the document using:

$('link[href="fos.css"]').remove();

And then you can add another into the head:

var newCss='fos2.css';
$('head').append('<link rel="stylesheet" type="text/css" href="'+newCss+'">');

or if you want to play nice:

var newCss='fos2.css';
$('<link>')
    .attr({
        rel: 'stylesheet',
        type: 'text/css',
        href: newCss
    })
    .appendTo('head');

I've created a simple jsFiddle test case for you.

Comments

0

Keep information about layout independent of the color, images etc.If possible, create one css styling for body tag which has information of color in it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.