473,385 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

style based on tag content

I'd like to change the color of text in a table data field based on the
content...

For example, I'd like all <td>pos</td> to show the "pos" in red. All
<td>pending</td> should show as the normal black. I don't really want
to have to add a class or a style, but that's how I've been doing it so
far...

I seem to remember that this is possible, I just can't remember the
syntax right now and I'm not having any luck with google...

Thanks in advance,

Jeff P.

Apr 27 '06 #1
18 3805
On 27 Apr 2006 11:17:13 -0700, jp******@rgs.uci.edu wrote:
I'd like to change the color of text in a table data field based on the
content...

For example, I'd like all <td>pos</td> to show the "pos" in red. All
<td>pending</td> should show as the normal black. I don't really want
to have to add a class or a style, but that's how I've been doing it so
far...

I seem to remember that this is possible, I just can't remember the
syntax right now and I'm not having any luck with google...


As you process the data to put it in the table, check to see what it
is and then give the <td> the appropriate class.

For example in php:

$myClass="";
if ($row->status == "pos")
$myClass="pos";
if ($row->status == "pending")
$myClass="pending";

$myData .= "<td class=\"$myClass\">$row->status</td>";

Ken

Apr 27 '06 #2
The data isn't in a database for this one... This data get changed
rarely, so it isn't really worth the extra complexity of stuffing
everyting in a db.

Also, it just seems like there should be a simple way to do it with
style sheets. But it looks like you can do styles with *tags*, but not
*content*. Which is a bummer.

For example, it would be nice to italicize "et. al." or "in vivo"
across a site without having to add <i>, or div or span tags?

Don't you think there should be something like:

td:content "pos" {
color: red;
}

or
content "in vivo" {
font: italic;
}

~Jeff P.

Apr 27 '06 #3
On 27 Apr 2006 12:20:03 -0700, jp******@rgs.uci.edu wrote:

Don't you think there should be something like:

td:content "pos" {
color: red;
}

or
content "in vivo" {
font: italic;
}


It's called XML.

Ken

Apr 27 '06 #4
On Thu, 27 Apr 2006 15:35:51 -0400, Ken Loomis
<no**************@address.com> wrote:
td:content "pos" {
color: red;
}

or
content "in vivo" {
font: italic;
}
It's called XML.


Actually, come to think of it, even XML can't do that.

It has to be done programatically, AFAIK.
Ken

Apr 27 '06 #5
The closest I can think of is attribute selectors.

http://www.w3.org/TR/CSS21/selector....bute-selectors

[att=val]
Match when the element's "att" attribute value is exactly "val".

However I don't think you can have content as the att. Also, this selector
is NOT cross browser friendly, as although my favourite browser (Firefox)
supports it, 90% of the world's browsers (Internet Explorer) do not.

Martin
<jp******@rgs.uci.edu> wrote in message
news:11*********************@i40g2000cwc.googlegro ups.com...
I'd like to change the color of text in a table data field based on the
content...

For example, I'd like all <td>pos</td> to show the "pos" in red. All
<td>pending</td> should show as the normal black. I don't really want
to have to add a class or a style, but that's how I've been doing it so
far...

I seem to remember that this is possible, I just can't remember the
syntax right now and I'm not having any luck with google...

Thanks in advance,

Jeff P.

May 2 '06 #6

Ken Loomis wrote:
It's called XML.
Actually, come to think of it, even XML can't do that.


Try XSLT
It has to be done programatically, AFAIK.


Yes, but that doesn't have to mean a procedural program language.
XSLT's declarative style makes it very easy.

It's pretty much trivial to take an XHTML document, then transform it
with XSLT to add suitable classes and style the results with CSS. The
transformed document can be practically identical to the original, just
with added classes.

Although you don't need to use XHTML to do this, it's notably easy to
do it with XSLT and a slightly modified XSLT identity transform.

May 2 '06 #7
VK

jp******@rgs.uci.edu wrote:
I'd like to change the color of text in a table data field based on the
content...

For example, I'd like all <td>pos</td> to show the "pos" in red. All
<td>pending</td> should show as the normal black. I don't really want
to have to add a class or a style, but that's how I've been doing it so
far...

I seem to remember that this is possible, I just can't remember the
syntax right now and I'm not having any luck with google...

Thanks in advance,
Use behaviors
IE 5.5 or higher
Firefox 1.0 or higher
Camino 1.0 or higher
any other modern Gecko based browser
Netscape 8.0 or higher

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Table 1</title>
<style type="text/css">
table {
border: medium groove;
-moz-binding: url(zebra.xml#default);
behavior: url(bycontent.htc);
}
td {
margin: 0px 0px;
border: 1px solid black;
padding: 5px 5px;
}
</style>
</head
<body <table
<caption>Content based styling</caption
<thead
<tr
<th>Header 1</th
<th>Header 2</th
<th>Header 3</th
</tr
</thead
<tfoot
<tr
<td>Footnote 1</td
<td>Footnote 2</td
<td>Footnote 3</td
</tr
</tfoot
<tbody
<tr
<td>pos</td
<td>Content 1-2</td
<td>Content 1-3</td
</tr
<tr
<td>Content 2-1</td
<td>pos</td
<td>Content 2-3</td
</tr
<tr
<td>Content 3-1</td
<td>Content 3-2</td
<td>at. al.</td
</tr
</tbody
</table </body
</html>

// bycontent.xml

<?xml version="1.0"?>
<bindings xmlns="http://www.mozilla.org/xbl">
<binding id="default">
<implementation>

<constructor><![CDATA[
var rL = this.rows.length;
for (var i=0; i<rL; i++) {
this.rows[i].style.backgroundColor = (i%2)? '#DDDDDD' : '#FFFFFF';
}
]]></constructor>
<destructor><![CDATA[
// your code here
]]></destructor>

</implementation>
</binding>
</bindings>
// bycontent.htc
<?xml version="1.0"?>
<public>
<component>
<attach event="oncontentready" onevent="constructor()" />
<attach event="ondetach" onevent="destructor()" />
</component>
<script type="text/Jscript"><!--

function constructor() {
var rL = element.tBodies[0].rows.length;
var cL = 0;
var cR = null;
for (var i=0; i<rL; i++) {
cL = element.tBodies[0].rows[i].cells.length;
for (var j=0; j<cL; j++) {
cR = element.tBodies[0].rows[i].cells[j];
switch (cR.innerText) {
case 'pos' :
cR.style.color = '#FF0000';
break;
case 'at. al.' :
cR.style.fontStyle = 'italic';
break;
default: /*NOP*/
}
}
}
}
function destructor() {
// your code goes here
}

//--></script>

</public>
P.S. The weird pretty-print in HTML is not some requirement for
behaviors. I just programmed my editor in this way to fix the phantom
nodes bug in Firefox. You are free to use the conventional pretty-print.

May 3 '06 #8
VK

VK wrote:
Use behaviors
IE 5.5 or higher
Firefox 1.0 or higher
Camino 1.0 or higher
any other modern Gecko based browser
Netscape 8.0 or higher
Here btw another popular demand: style differently odd and even table
rows:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Table 1</title>
<style type="text/css">
table {
border: medium groove;
-moz-binding: url(zebra.xml#default);
behavior: url(zebra.htc);
}
td {
margin: 0px 0px;
border: 1px solid black;
padding: 5px 5px;
}
</style>
</head
<body <table
<caption>&quot;Zebra&quot; row style</caption
<thead
<tr
<th>Header 1</th
<th>Header 2</th
<th>Header 3</th
</tr
</thead
<tfoot
<tr
<td>Footnote 1</td
<td>Footnote 2</td
<td>Footnote 3</td
</tr
</tfoot
<tbody
<tr
<td>Content 1-1</td
<td>Content 1-2</td
<td>Content 1-3</td
</tr
<tr
<td>Content 2-1</td
<td>Content 2-2</td
<td>Content 2-3</td
</tr
<tr
<td>Content 3-1</td
<td>Content 3-2</td
<td>Content 3-3</td
</tr
</tbody
</table </body
</html>

// zebra.xml

<?xml version="1.0"?>
<bindings xmlns="http://www.mozilla.org/xbl">
<binding id="default">
<implementation>

<constructor><![CDATA[
var rL = this.rows.length;
for (var i=0; i<rL; i++) {
this.rows[i].style.backgroundColor = (i%2)? '#DDDDDD' : '#FFFFFF';
}
]]></constructor>
<destructor><![CDATA[
// your code here
]]></destructor>

</implementation>
</binding>
</bindings>
// zebra.htc

<?xml version="1.0"?>
<public>
<component>
<attach event="oncontentready" onevent="constructor()" />
<attach event="ondetach" onevent="destructor()" />
</component>
<script type="text/Jscript"><!--

function constructor() {
var rL = element.rows.length;
for (var i=0; i<rL; i++) {
element.rows[i].style.backgroundColor = (i%2)? '#DDDDDD' : '#FFFFFF';
}
}
function destructor() {
// your code goes here
}

//--></script>

</public>

May 3 '06 #9
VK

VK wrote:
Use behaviors
Damn... Copying - Pasting code is a real challenge for weak minds like
mine... :-)
-moz-binding: url(zebra.xml#default);
behavior: url(bycontent.htc);


of course:
-moz-binding: url(bycontent.xml#default);
behavior: url(bycontent.htc);

and respectively bycontent.xml is:

<?xml version="1.0"?>
<bindings xmlns="http://www.mozilla.org/xbl">
<binding id="default">
<implementation>

<constructor><![CDATA[
var rL = this.rows.length;
var cL = 0;
var cR = null;
for (var i=0; i<rL; i++) {
cL = this.rows[i].cells.length;
for (var j=0; j<cL; j++) {
cR = this.rows[i].cells[j];
switch (cR.innerHTML) {
case 'pos' :
cR.style.color = '#FF0000';
break;
case 'at. al.' :
cR.style.fontStyle = 'italic';
break;
default: /*NOP*/
}
}
}
]]></constructor>
<destructor><![CDATA[
// your code here
]]></destructor>

</implementation>
</binding>
</bindings>
In my code I actually add innerText virtual property to the behavior
(binding) for more uniform looking and reliable code. If you don't use
HTML tags in your table cells like <span>pos</span> then the above is
good enough.

May 3 '06 #10
Well, yes it will work, but it's a lot of overhead. In this case I'd
rather just add a class to the columns I want made red, though.

I think it's better to keep things simple. If I went for your solution
(which *will* work) I'd have to convert all the HTML documents I have
into XHTML, and then set up XSLT to do what I want. Doesn't that seem a
bit much?

I'm changing the subject slightly, but doesn't it seem like one should
be able to use a simple CSS technique (like 'td:content "pos"' or
'content "in vivo"' from an earlier post)?

Don't get me wrong, I'm not expecting anyone here to add this feature
to CSS. I'm simply pointing out that, now that I think about it, it
looks like a shortcoming of CSS... CSS is ultimatly about styling
content, isn't it?

I think there should be a way to directly style *content*, not just
*tags*.

May 3 '06 #11
Thank you, that's what I was thinking of...

May 3 '06 #12
That's the best solution so far. I'll have to come up with a set of
behaviors for our site.

Thank you!

May 3 '06 #13
Martin Eyles wrote:
The closest I can think of is attribute selectors.

http://www.w3.org/TR/CSS21/selector....bute-selectors

[att=val]
Match when the element's "att" attribute value is exactly "val".

However I don't think you can have content as the att.


Just to confirm: you can't. An element's content isn't an attribute.
May 3 '06 #14
VK

jp******@rgs.uci.edu wrote:
That's the best solution so far. I'll have to come up with a set of
behaviors for our site.

Thank you!


You are welcome. I have some set of copyright free twinpairs (XBL/HTC)
for some most common task. Ask if you need.

May 3 '06 #15
VK wrote:
jp******@rgs.uci.edu wrote:
I'd like to change the color of text in a table data field based on the
content...

For example, I'd like all <td>pos</td> to show the "pos" in red. All
<td>pending</td> should show as the normal black. I don't really want
to have to add a class or a style, but that's how I've been doing it so
far...

I seem to remember that this is possible, I just can't remember the
syntax right now and I'm not having any luck with google...

Thanks in advance,


Use behaviors
IE 5.5 or higher
Firefox 1.0 or higher
Camino 1.0 or higher
any other modern Gecko based browser
Netscape 8.0 or higher


[demo snipped]

Seems like an awful lot of work to use a behavior for this. Wouldn't it
be easier to take the working part of it and stick it in a script?

function adjustTable(table)
{
var tds = table.getElementsByTagName("td");
var iTD;
for (iTD = 0; iTD < tds.length; iTD++)
{
switch ...

}
}

[in onload handler]
adjustTable(document.getElementById("myTable"));

If the scope should be restricted to particular columns, then you
approach of going methodically through the TBODY and then TRs makes sense.
May 3 '06 #16
VK

Harlan Messinger wrote:
Use behaviors
IE 5.5 or higher
Firefox 1.0 or higher
Camino 1.0 or higher
any other modern Gecko based browser
Netscape 8.0 or higher
[demo snipped]

Seems like an awful lot of work to use a behavior for this. Wouldn't it
be easier to take the working part of it and stick it in a script?

function adjustTable(table)
{
var tds = table.getElementsByTagName("td");
var iTD;
for (iTD = 0; iTD < tds.length; iTD++)
{
switch ...

}
}

In this particular case it is replaceable by <script>, because we do
not have to augment elements by new properties/methods, do not insert
anonymous content, do not require refactored events for bounds elements
etc.

In the "Sticky Notes" sample
<http://developer.mozilla.org/en/docs/XBL:XBL_1.0_Reference#Example_-_Sticky_Notes>

we have a HTML 4.01 Strict valid document with notes decorated with SVG
(Gecko) / VML (IE) graphics being part of the document tree (and more).

Also "a lot of work" for who? ;-) Any solution requires some initial
work, sometimes a lot of work - all eventually expressed in one line
<script src="myscript.js"></script>.
The question is how easy is to raise the solution over and over again
without adjusting it for new context every time. In this aspect to have
everything in one .css sheet with two lines added:
table {
....
-moz-binding: url(zebra.xml#default);
behavior: url(zebra.htc);
}
doesn't need a lot of work.

If the scope should be restricted to particular columns, then you
approach of going methodically through the TBODY and then TRs makes sense.


Well... Only to you alone: a dark secret of XBL :-) In the current
Gecko implementation in tables one can attach bindings only to the
table element itself, not to tr or td (bug 83830). IE behavior doesn't
have this limitation. Without this bug I would attach bindings right to
rows which has much more sense - and would let me stay out of any
coding at all.

May 3 '06 #17
No, it doesn't work. Yes, that's what I was originally thinking of.

As things stand now, there is no easy solution.

May 3 '06 #18
>> Running Apple programs on a PC doesn't work, even if you were
thinkig of using an emulator

jp******@rgs.uci.edu wrote:
No, it doesn't work. Yes, that's what I was originally thinking of.

As things stand now, there is no easy solution.

Is your car running? Or maybe you could take public transportation?
Otherwise, how are you going to get to work?

jp******@rgs.uci.edu wrote:
No, it doesn't work. Yes, that's what I was originally thinking of.

As things stand now, there is no easy solution.

Such a radical change is likely to cause problems. Maybe we should
consider a more measured approach.

jp******@rgs.uci.edu wrote:
No, it doesn't work. Yes, that's what I was originally thinking of.

As things stand now, there is no easy solution.

Sorry - I'm trying...
May 3 '06 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

72
by: Herbert | last post by:
I'm still relativey new to stylesheets, so I'm hoping that the way I'm going about things can be seriously improved upon, i.e . I just haven't undersood something obvious about the 'cascading'...
1
by: Scanner2001 | last post by:
I am trying to insert a printer page break inside of a form. I do not seem to be able to do this. My form contains 3 asp panels. Each of the panels have dynamically built content, and they are...
14
by: laurence | last post by:
I am implementing a comprehensive image-map generator utility, so have been studying W3C HTML 4.01 Specification (http://www.w3.org/TR/html4/struct/objects.html#h-13.6) on image maps (among other...
5
by: STeve | last post by:
Hey guys, I currently have a 100 page word document filled with various "articles". These articles are delimited by the Style of the text (IE. Heading 1 for the various titles) These articles...
10
by: p3t3r | last post by:
I have a treeview sourced from a SiteMap. I want to use 2 different CSS styles for the root level nodes. The topmost root node should not have a top border, all the other root nodes should have a...
5
by: MarkW | last post by:
I hope this is the correct place to post this: I am developing a web site for a e-commerce business I will be running. The site I'm setting up will be 50% store, 50% content. I'm not sure which...
3
by: =?Utf-8?B?dG9t?= | last post by:
Hi All, I have a theme with a Style.css in which I have defined a style rule for the body element : body { min-width: 780px; margin: 0; padding: 0;
10
by: Terrence Brannon | last post by:
Hello, The most common way of dynamically producing HTML is via template engines like genshi, cheetah, makotemplates, etc. These engines are 'inline' --- they intersperse programming...
1
by: DelphiCoder | last post by:
I have the following sample: <html> <body> <div id="mydiv" style="position: absolute;"> Hello </div> <script> alert(document.getElementById("mydiv").style.top); </script> </body> </html> ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.