Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2009
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    document.write("style..........

    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.

  • #2
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    It works fine for me
    Code:
    <!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">
    <head>
       <meta http-equiv="content-type" content="text/html;charset=utf-8" />
       <title>Template</title>
    </head>
    <body>
       <script type="text/javascript">
          document.write("<div style='background-color:black;'>&nbsp;</div>");
       </script>
    </body>
    </html>
    I am still learning English

  • #3
    New Coder
    Join Date
    Jul 2009
    Posts
    21
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Try this...
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head>
    	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    	<title>Untitled</title>
    </head>
    <body>
    <script type="text/javascript">document.write("<div style='background-color:black;color:#fff'>Some text</div>");</script>
    </body>
    </html>
    Amphiluke was faster

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,564 Times in 2,542 Posts
    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!

  • #5
    New Coder
    Join Date
    Jul 2009
    Posts
    21
    Thanks
    1
    Thanked 4 Times in 4 Posts
    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:
    Code:
    document.write('<div style="background-color:black">Some text</div>');
    Code:
    document.write("<div style='background-color:black'>Some text</div>");
    Or escape the quotation marks inside the function:

    Code:
    document.write('<div style=\'background-color:black\'>Some text</div>');
    Code:
    document.write("<div style=\"background-color:black\">Some text</div>");
    Whichever you choose - it should work


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •