I am able to incorporate CSS into the HTML, like this:

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

How can I incorporate CSS into javascript?

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

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

Thanks

Recommended Answers

All 4 Replies

check this is working:

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

As Shanti said, the fix is to escape your double quotation in your string. The way you do creates an unterminated string.

/*
document.writeln("<tr><td style="background-color:#FFFF00;">" + "test" + "</td></tr>");
|----------------|--------------|-------------------------|-|
^                ^              ^            ^            ^ ^
|                |              |            ^            | |
start            start          end        expect         start
command          string         string     expression     string
                                                            end
                                                            string
*/

Use jQuery. It is a complete waste of time to use document.writeln() and it doesn't lend itself to dynamic behaviors.

eg,

html: <tr><td id='demoItem'>blah blah</td></tr>

javascript: $('#demoItem').css('background', '#FFFF00');

If you learn to use jQuery, you gain access to a million useful utilities and functions. Go for it!!

Member Avatar for Pnorq

I totally agree with tqwhite above. The time spent learning JQuery will pay huge dividends. Just for clarity, what JQuery is doing under the covers is essentially this (using tqwhite's example):

html: <tr><td id='demoItem'>blah blah</td></tr> writing the JavaScript yourself:

var el = document.getElementById("demoItem");
el.style.backgroundColor = "#FFFF00";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.