views:

92

answers:

1

Hi All..

I am comparing between two tables first column each. If there is find a match i am copying the text from the adjacent cell of the first table to the second table. I am able to compare strings and get the value, but finding it difficult to print it in the second table. I am getting the value in the var "replaceText", but how to print it in the second table ?? Please help... Sample code is as follows..

<script type="text/javascript">
    jQuery.noConflict();
    jQuery(document).ready(function(){

    jQuery('.itemname').each(function(){
    var itemName = jQuery(this).text();

        jQuery('.comparerow').each(function() {
        var compareRow = jQuery(this).text();

            if (itemName == compareRow) {
            var replaceText = jQuery(this).next('td').text();
            alert(replaceText);
            }

        });
    }); 
    });
</script>

HTML is as follows

<table width="100%"><thead>
<tr>
  <th align="left" >Name</th><th>Description</th></tr></thead>
<tbody>

<tr>
  <td class="comparerow">IX0001</td>
  <td class="desc">Desc 1 </td>
</tr>


<tr>
  <td class="comparerow">IX0002</td>
  <td class="desc" >Desc 2 </td>
</tr>

<tr>
  <td class="comparerow">IX0003</td>
  <td class="desc">Desc 3 </td>
</tr>
<tr>
  <td class="comparerow">IX0004</td>
  <td class="desc">Desc 4 </td>
</tr>
</tbody>
</table>

<br />

<table width="100%">
<tr>
<th>Name</th><th>Description</th>
</tr>
<tr >
<td class="itemname">IX0001</td><td></td>
</tr>
<tr>
<td class="itemname">IX0002</td><td></td>
</tr>
<tr>
<td class="itemname">IX0003</td><td></td>
</tr>
</table>
A: 

You can simplify this overall using the :contains selector to find your match like this:

jQuery.noConflict();
jQuery(document).ready(function() {
  jQuery('.itemname').each(function() {
    var itemName = jQuery(this).text();
    var match = jQuery('.comparerow:contains("' + itemName + '")');
    if(match.length)
      jQuery(this).next('td').text(match.next('td').text());
  }); 
});

This loops through each item, looks for a match. If we found a match (.legnth is > 0) then set the next <td> text to the match's next <td> text.

Nick Craver
@Nick Thanks for the answer.. I am getting "Missing )" error in line number 5
Sullan
@Sullan - I'm not seeing this, double check any code this may be wrapped in? Also, can you see which actual line of code (sometimes the number isn't accurate)?
Nick Craver
@Nick Craver Now it works, but the text which is printing is "IX001" and not the next column value which is "Desc 1"...
Sullan
@Sullan - I left out the next when first answering the question, make sure you have `match.next('td').text()` and not `match.text()`
Nick Craver
@Nick Craver Thanks a lot... it worked like a charm.... :-)
Sullan
@Nick Craver Now i am facing a problem with a different logic. Suppose if i have a value "IX0002" twice in the first table, then description table in the second column will have values like "Desc 2Desc 2" repeated twice.. how do i fix this issue.. ??
Sullan
@Sullan - In that case change this `jQuery('.comparerow:contains("' + itemName + '")');` to this: `jQuery('.comparerow:contains("' + itemName + '"):first');` let me know if that doesn't solve it for you :)
Nick Craver
@Nick Craver .. thanks it works on fine.. but i am getting a script error because the first table has around 1000 columns and this is showing a script error message. Following is the new problem and half the soution for it.. pls help (http://stackoverflow.com/questions/2526003/copying-values-of-one-table-to-another-how-to-convert-this-js-function-to-jquery)
Sullan