views:

68

answers:

3

Here's a sample table

<table width="580" height="217" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="334">Website</td>
    <td width="246">Category</td>
  </tr>
  <tr>
    <td>http://www.google.com&lt;/td&gt;
    <td>Search Engine</td>
  </tr>
  <tr>
    <td>http://www.gmail.com&lt;/td&gt;
    <td>Web Mail</td>
  </tr>
  <tr>
    <td>http://www.xyz.com&lt;/td&gt;
    <td></td>
  </tr>
  <tr>
    <td>http://www.amazon.com&lt;/td&gt;
    <td>Shopping</td>
  </tr>
  <tr>
    <td>http://www.website.com&lt;/td&gt;
    <td></td>
  </tr>
</table>

Two cells are empty in this table, how can i use jquery to add "not found" text in those empty cells?

+5  A: 
$('table td:empty').append('Not Found');

Demo here

Note that the selector :empty means no children, including text nodes, so watch how your html is laid out as laying out the opening and closing tags on new lines may introduce a text node child into what you consider an empty cell. A basic way to combat this could be to define our own selector expression

(function ($) {

    $.extend($.expr[':'],{
        reallyEmpty: function(elem) {
            return !elem.firstChild ||
               elem.firstChild.nodeType === 3 && !!/^\s|\n$/.test(elem.firstChild.textContent);
        }
    });

})(jQuery);

and then use this

$('td:reallyEmpty').append('Not Found');
Russ Cam
+1 Short and sweet. Like Rhea Perlman.
jessegavin
nice :) I wasn't aware of that syntax
KennyCason
Thank you very much, can you refer me some links where i can learn more about jquery in depth (other than jquery.com and w3cschools)
Roccos
Some books I can recommend - jQuery in Action (http://manning.com/bibeault2/), jQuery cookbook (http://oreilly.com/catalog/9780596159788). It's useful to download the book source too and go through that. Both books are good for learning good practices. Outside of that, I'd look at http://stackoverflow.com/questions/749674/jquery-samples to begin. Set yourself up an RSS feed account and subscribe to those sites you find useful :)
Russ Cam
Thanks, I will certainly do that :)
Roccos
A: 

Try this

$('td').each(function(){
    if($(this).html()=='') 
       $(this).html('not found');
   });
Bang Dao
Hehe, you beat me to it :)
KennyCason
Since all of your answers worked perfectly, Russ Cam snippet is very short and gets the job done.Thank all for your support.
Roccos
+1  A: 

Here is a snippet I put together.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"&gt;&lt;/script&gt;
<script type="text/javascript" >

$(document).ready(function() {

$('#tableid td').each(function()
{
  if($(this).html() == "") {
    $(this).html("Not Found"); 
  }
});

});

</script>
<table id="tableid" width="580" height="217" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="334">Website</td>
    <td width="246">Category</td>
  </tr>
  <tr>
    <td>http://www.google.com&lt;/td&gt;
    <td>Search Engine</td>
  </tr>
  <tr>
    <td>http://www.gmail.com&lt;/td&gt;
    <td>Web Mail</td>
  </tr>
  <tr>
    <td>http://www.xyz.com&lt;/td&gt;
    <td></td>
  </tr>
  <tr>
    <td>http://www.amazon.com&lt;/td&gt;
    <td>Shopping</td>
  </tr>
  <tr>
    <td>http://www.website.com&lt;/td&gt;
    <td ></td>
  </tr>
</table>
KennyCason