3

I am really confused here.. I am trying to get the values from a table and add those values to the div with class title using append function and .text function. The problem is that when I use the each function the last value in table gets appended and the rest are skipped.. This is my code:

<script type="text/javascript">
    $(document).ready(function() {
        $('#nameGridView tr').each(function() {
            if (!this.rowIndex) return; // skip first row
            var productName = this.cells[0].innerHTML;
            $('.title').each(function (i, obj) {
                $('.title').html(productName);

            });

        });
    });

</script>

Please tell me how to iterate through each table and add simultaneously, like say for first div title, the first value from table gets appended and so on. thanks.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
designerNProgrammer
  • 2,621
  • 5
  • 34
  • 46

1 Answers1

-1

Use append() to append to an element. Also use .eq() to get the div with the same index as the row.

<script type="text/javascript">
    $(document).ready(function() {
        var $title = $('.title');
        $('#nameGridView tr').each(function(i) {
            if (!this.rowIndex) return; // skip first row
            var productName = this.cells[0].innerHTML;
            $title.eq(i-1).append(productName);            
        });
    });

</script>
Musa
  • 96,336
  • 17
  • 118
  • 137
  • well there are many divs. the same divs as the rows cells in the table to append to... please!! help me.. i really need this... – designerNProgrammer Jan 16 '13 at 16:05
  • i wish i could give you credits here but i am am a newbie and my points are low so i cant but as soon as i have i will definately do it..:) – designerNProgrammer Jan 16 '13 at 16:20
  • @RyanComputerProgrammer to give credit you can accept this as the answer by clicking the tick next to the answer. – Musa Jan 16 '13 at 16:36