Add Stylesheet Dynamically to the Web page Using Jquery

To change the layout of the web  page we can use multiple style sheets.  The following jquery code allows the user to change the view of the current page.

jquery_logo

$(document).ready(function(){ 
     $('#Layout2').click(function(){ 
         var newcss = '<link type="text/css" rel="stylesheet" href="style2.css" />';
         $('head').append(newcss);
     });
     $('#Layout1').click(function(){
         $('link[href="style2.css"]').remove();
     });
});

And the HTML is

<div id="Layout1">Layout1</div>
<div id="Layout2">Layout2</div>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s