javascript beginner: add a dynamic style in javascript?

css, javascript

Solution

Your code is fine.But i think that you are not calling it on right time, so call it when your body tag is loaded

 window.addEventListener('DOMContentLoaded',function(){
    var sheet = document.createElement('style'); 
    sheet.type="text/css"; 
    sheet.innerHTML = "body{color:red;}"; 
    document.body.appendChild(sheet);
    }, false);

Problem

Possible Duplicate: How to create a <style> tag with Javascript I write a dynamic form using java-script. I want to add the style in java-script itself. I write some code to add a style. But it is not working.This is the code am write in my project. ``` var sheet = document.createElement('style'); sheet.type="text/css"; sheet.innerHTML = "body{color:red;}"; document.body.appendChild(sheet); ``` Anyone help me please.

Original source

Related problems