Note that there are some explanatory texts on larger screens.

plurals
  1. POTransforming tablecell content with CSS or jQuery
    primarykey
    data
    text
    <p>I have a html table similar to this:</p> <pre><code>&lt;table&gt; &lt;tr&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;Some text...&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;2&lt;/td&gt; &lt;td&gt;Some text...&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;3&lt;/td&gt; &lt;td&gt;Some text...&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;Some text...&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;Some text...&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;2&lt;/td&gt; &lt;td&gt;Some text...&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;3&lt;/td&gt; &lt;td&gt;Some text...&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre> <p>The cells in the first column contain a numeric value of 1, 2 or 3. </p> <p>I’m looking for a neat way of transforming this value into something more “graphical” using a client side approach such as CSS (preferably) or jQuery. For example, instead of “1” the cell content should be rendered as an icon, or a dot with a red color. Just setting the background color would also be ok.</p> <p><strong>UPDATE:</strong></p> <p>Thanks for all the suggestions. It seems it was just the Each method from jQuery I was missing. :)</p> <p>Here's my final code. I wrapped it in separate function which is called on document ready and after table updates.</p> <pre><code>function fnTransformIcons() { var color; $("td.Icon").each(function() { var value = $(this).html(); switch (parseInt(value)) { case 0 : color = 'white'; break; case 1 : color = 'red'; break; case 2 : color = 'green'; break; case 3 : color = 'yellow'; break; } $(this).css("backgroundColor", color); }); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload