View Full Version : document.write("style..........

07-28-2009, 09:23 PM
I am trying to write a div with a black background using document.write, but i dont know how to make the style work because just doing style='background-color:black;' isnt working in document.write. thanks in advance. I am kind of new to javascript so be gentle. thanks in advance.

07-28-2009, 09:46 PM
It works fine for me

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<script type="text/javascript">
document.write("<div style='background-color:black;'>&nbsp;</div>");

07-28-2009, 09:49 PM
Try this...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">document.write("<div style='background-color:black;color:#fff'>Some text</div>");</script>

Amphiluke was faster:p

Philip M
07-28-2009, 09:50 PM
It works for me.

document.write("<div id = 'div1' style='color:white;background-color:black;'>ABCDEF</div>");

"A little sincerity is a dangerous thing, and a great deal of it is absolutely fatal". - Oscar Wilde (Irish Poet, Novelist, Dramatist and Critic, 1854-1900)

Amphiluke and BlakeG were faster! :)

07-28-2009, 10:01 PM
I think you haven't escaped the single or double quotation marks.

You can try the following codes:

Use different quotation marks inside the document.write function:

document.write('<div style="background-color:black">Some text</div>');

document.write("<div style='background-color:black'>Some text</div>");

Or escape the quotation marks inside the function:

document.write('<div style=\'background-color:black\'>Some text</div>');

document.write("<div style=\"background-color:black\">Some text</div>");

Whichever you choose - it should work ;)