Sunday, September 29, 2013

adding element with a specific style using appenchild method

this is my code :

(a=document).getElementsByTagName(‘body’)[0].appendChild(a.createElement(‘div’).style.cssText="someCssStyle");

it doesn’t work !
but when i write only this :

(a=document).getElementsByTagName(‘body’)[0].appendChild(a.createElement(‘div’));

it works, so why i can’t add the div element with a specific style ?
what’ wrong with my work .. thanks in advantage
i want to add a div element with a specific style just from the URL implantation on chrome using :

javascript://all of my code goes here

so it must be short

a.createElement(‘div’).style.cssText=”someCssStyle”

This will return “someCssStyle” which will be given as argument to (a=document).getElementsByTagName(‘body’)[0].appendChild( function. So the div is never added. Can you see the problem here ?

You have to create the div, style it & then add it to the body. Like this

var div = document.createElement("div");div.style.cssText = "someCssStyle";document.body.appendChild(div);

No comments:

Post a Comment