Ed Nez Ed Nez - 1 year ago 191
jQuery Question

Javascript - Add and Remove CSS file with one button

I created a button that ADD CSS file to the page header.
How Can I REMOVE this CSS from the header with the second click on the same button?


var ls = document.createElement('link');


<button id="myButton">Add / Remove</button>

Answer Source

Just add something to identify the style tag, an ID seems appropriate

    $("#myButton").on('click', function(){

        if ( $('#myStyle').length === 0 ) { // does not yet exist

            $('<link />', {
                id   : 'myStyle',
                rel  : 'stylesheet',
                href : 'myCSSfile.css';

        } else { // exists, remove it


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download