View Full Version : CSS with Javascript??

03-04-2011, 01:08 AM
I am able to incorporate CSS into the HTML, like this:

<tr><td style="background-color:#FFFF00;">test</td></tr>

How can I incorporate CSS into javascript?

document.writeln("<tr><td style="background-color:#FFFF00;">" + "test" + "</td></tr>");

The javascript code doesn't work or this is it even possible?


03-04-2011, 01:57 AM
You are using quotes " to tell Javascript where the string ends. So when you also use it to start and end the HTML property, it makes things confusing and it doesn't parse correctly.

Simply replace the embedded "s with \".

document.writeln("<tr><td style=\"background-color:#FFFF00;\">" + "test" + "</td></tr>");

Alternatively you can alternate single and double quotes. It looks a bit more elegant, though it can be a little harder to remember.

document.writeln('<tr><td style="background-color:#FFFF00;">' + 'test' + '</td></tr>');