SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    CSS with Javascript???

    I am able to incorporate CSS into the HTML, like this:
    Code:
    <tr><td style="background-color:#FFFF00;">test</td></tr>
    How can I incorporate CSS into javascript?
    Code:
    document.writeln("<tr><td style="background-color:#FFFF00;">" + "test" + "</td></tr>");
    The javascript code doesn't work or this is it even possible?

    Thanks

  2. #2
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    23,599
    Mentioned
    411 Post(s)
    Tagged
    7 Thread(s)
    This is a JavaScript question (some moved to that forum )

    Inline code/styles like that are not the best practice, but I believe this would work, though I'm no JavaScripter:

    Code:
    <table>
    <script type="text/javascript">
    document.write("<tr><td style='background-color:#FFFF00;'>" + "test" + "</td></tr>")
    </script>
    </table>

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,604
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    You shouldn't really be using document.write since it is far less flexible than any of the alternatives (such as using innerHTML or DOM calls).

    Also it would probably be better if you had that CSS in a separate file allocated to a class and then just added the class to the <td>
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Addict AtSea webdesign's Avatar
    Join Date
    May 2010
    Location
    Newark, Delaware
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can add a class via the DOM.

    Code:
    document.getElementById("blah").className = "cssclass";
    So if you had your style created in your css stylesheet and wanted to apply a style to that table cell, add a class to the td using the corresponding ID.


Bookmarks

Posting Permissions

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