treehouse : what would you like to learn today?
Web Design Web Development iOS Development

How to add an external CSS file to a page with Javascript

  • I'm wondering how it would be done to have an input field where you can paste a link to an external stylesheet from anywhere, and click a submit button and have that stylesheet be applied to the page it's on, very similar to how you can do on codepen.io where you can add in your own external stylesheet. Any help would be appreciated. Thanks!

  • you could use the "append()" function in jquery to add it to the element...

    eg:

    
    $('button#addCss').on('click', function() {
       var uri = $('input#cssUri').val();
       $('head').append(uri);
    });
    
  • Cool, I'll have to give that a try. Thanks!