954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Coloring cetain texts within a table

Hi. I am creating an asp.net web site. Using GridView the website creates a table. Now my question is I want to write a javascript function that colors the text "NO" red within the table cells. Waiting for your reply.

johnroach1985
Junior Poster
138 posts since Apr 2004
Reputation Points: 11
Solved Threads: 0
 

Why do you need Javascript if all you want to do is color the text? You can very well use CSS to do the same.

<html>
<head>
    <title>Example</title>
</head>
<body>
    <table style="border: 1px solid green; color: red">    
        <tr>    
            <td>Hello to all</td>
            <td>This is some dummy text</td>
        </tr>
    </table>
</body>
</html>
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Hi. I only want to color the cells containing the word "NO" not the other ones.... can this too be done with CSS?? Thanks in advance

johnroach1985
Junior Poster
138 posts since Apr 2004
Reputation Points: 11
Solved Threads: 0
 

Seeing that you are using ASP, you can use the ASP constructs to do selective rendering.

<html>
<head>
    <style>
        .wrong
        {
            color: red;
        }
        .right
        {
            color: green;
        }
    </style>
    <title>Example</title>
</head>
<body>
    <table style="border: 1px solid green;">
    <tr>
    <%
        dim i
        for i = 1 to 6
           if text = 'NO' then
                response.write("<td class='wrong'>" & text & "</td>")
           else
                response.write("<td class='right'>" & text & "</td>")
           end if
        next
    %>
    </tr>
    </table>
</body>
</html>


Though I am not sure on the ASP code I have written, this is pretty much what you would have to do. This will be either Javascript code or ASP code depending on where the data is coming form i.e. client side or server side.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Hi.Thank you for your prompt response. However there is still a problem. My table being dynamic not only grows in columns but in rows also. Is there no way to reach the elements within the cell and do a

if(document.cell=="NO") {
color.cell(red);
}


Or is there a way to reach the cell from GridView (I am using ASP.Net) Any help would be welcome. Thanks again and waiting for your reply...

johnroach1985
Junior Poster
138 posts since Apr 2004
Reputation Points: 11
Solved Threads: 0
 

It would be really better if you post the same question with the CSS solution I have provided to you to the ASP.NET section since this has ceased to be a CSS problem and is more of ASP thing.

Once you get what you are looking for, using the class attributes and applying the style of your choice is pretty easy.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Ok. Thank you...

johnroach1985
Junior Poster
138 posts since Apr 2004
Reputation Points: 11
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You