charan kamma charan kamma - 1 year ago 62
CSS Question

The margins in div of new tab not at all taking effect

Hi I am creating dynamic form in new tab using JavaScript. Margin effects on div are not taking effect. I am neither able to set the margins using class and id in css nor using div.setAttribute. Below is the code.

var openWindow ='dynForm.html','_blank','');
var windoc=openWindow.document;
windoc.write("This is dynamic form ");
var div=document.createElement("div");

`Only background color is taking effect on the new tab. The below screen shot shows how the screen looks irrespective of the values of margin.enter image description here

Quesstion 2: No class or id attribute in my css is being applied to div.



I tried"myDIV2";

I also tried
;` I applied the same using class also, but still couldn't find any difference. I do not know why its not working and what went wrong here. Please help

Answer Source

You're overriding the styles every time you call setAttribute, try combining the styles and then set the style attribute only once:

var openWindow ='dynForm.html','_blank','');
    var styles= [
      "margin: 1cm;",
    var windoc=openWindow.document;
    windoc.write("This is dynamic form ");
    var end_styles = "";
    for (var i=0; i<styles.length;i++) {
        end_styles += styles[i];
    var div=document.createElement("div");
    div.setAttribute("style", end_styles);

Note: the fiddle won't work on stackoverflow, I used it because it's easier to format code

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