tags:

views:

36

answers:

1

Hi guys . So I have this (huge) table , classed with css (big_conv_tbl) . All "empty" cells have a &nbsp inside them , I'm also using an odd/even css script to make the table easier to read (and thus some "empty" cells have a gray background color) .

What I want is to remove the borders and the background-color (set it to white) if the cell is "empty" (in my case if it contains "&nbsp") . Empty-cell:hide does not work .

I wrote a simple jquery script but it doesn't work :

$(".big_conv_tbl tr").each(function() {                           

var VC = $(this).find("td").html().trim();                          

if (VC == ''){                 
    $(this).css('background-color','white');
}             
});

What am I doing wrong ? I have no classes set for td's , only for rows (odd/even) . Please don't make me class every TD , the table is bloody huge . I appreciate any help

le :

Thanks Nick , I had to edit (not enough char. in comment) .

Here is a simple html page with a small table . For some reason it just won't work

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" >
$(".big_conv_tbl tr td").each(function() {
  if ($.trim($.text([this])) == '') {
    $(this).css('background-color','white');
  }
});
</script>


</head>


<body>
<table width="100%" border="1" class="big_conv_tbl" bgcolor="#999999">
  <tr>
    <td>asdsads</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>sagasag</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>sagsagsagsag</td>
    <td>&nbsp;</td>
  </tr>
</table>
</body>
</html>
+1  A: 

Instead of looping for the row, you need to loop over each cell, like this:

$(function() { //so it runs when the DOM is ready
  $(".big_conv_tbl tr td").each(function() {
    if ($.trim($.text([this])) == '') {
      $(this).css('background-color','white');
    }
  });
});

In this case we're using $.trim() on the $.text() of the object, rather than the .html() (which would still have content (the "&nbsp;"). $.text() is just a more efficient way to get the text out, since we don't need to create another jQuery object here.

Nick Craver
Thanks for the quick response . Silly of me to forget "td" but it still doesn't work :(le : still won't work . I'll try on another table
andrei
@andrei - Did you change the rest like my example, or just add the `td` to your original code? :)
Nick Craver
It seems $(".big_conv_tbl tr td").each(function() doesn't go through each td .
andrei
@andrei - In that case, I need to see your table's HTML :)
Nick Craver
@Nick please see the example I posted above
andrei
@andrei - Ah, your problem is it's running too early :) wrap it in a `document.ready` handler like my updated answer above :)
Nick Craver
Thank you very much Nick , works wonderful . But in my Drupal setup it doesn't work , like .css has higher priority than .js
andrei
@andrei - That shouldn't be the case, you getting any errors or anything? What version of jQuery?
Nick Craver
@Nick ver. 1.2.6
andrei
@andrei - Ah, 1.2.6 had a different text function, hadn't been optimized yet, change the `if` line to this: `if ($.trim($(this).text()) == '') {`
Nick Craver
Yep , thank you very much Nick . Have a good life :)
andrei