Jquery .Each function not working properly

I want to add a column to my table. My js code is as follows:

jQuery(document).ready(function () {

     jQuery('#addCol').click(function () {

        var countRows = $('#blacklistgrid tr').length;

        $('.class').each(function() {
            $( this ).append("<td><input type=\"text\"/></td>");
        });
    });

This code can be found in the fiddle. What am I doing wrong ?

Answers 1

  • You're targeting the wrong element.

    You should do this:

    jQuery('.Row').each(function() {
          jQuery( this ).append("<td><input type=\"text\"/></td>");
    });
    

    Notice jQuery('.Row') instead of jQuery('.class')

    See the docs, the class selector is used like jQuery('.<classname>'), and in this case you want to get each rows, which are identified by Row class


Related Articles