user2864691
user2864691

Reputation:

Javascript/JQuery iterate through table rows and cells and output an attr of checked checkboxes to console

I have the following code, which should go through each table row and dump my array which is declared in an earlier segment of javascript. Then if the checkbox is checked, and it has an attr of "changed=yes" then it should be pushed onto the array and the value should be outputted in console as well as the "path" attribute which should be outputted as a variable that can be overwritten every time the function finds a new checkbox that is checked and changed. So what is wrong with my code? These functions are contained in a function that is called when the user clicks submit on the form.

JsFiddle: http://jsfiddle.net/hU89p/392/

        $('#myTable1 tr').each(function(){
        myArray = [];
                    $.each($("input[type='checkbox']:checked").closest("td").siblings("td"),
                    function () {
                    if($(this).data("changed") == 'yes'){
                        myArray.push($(this).attr('checkboxtype'));
                        filepath = $(this).attr('path');
                        console.log(myArray);
                        console.log(filepath);
                    }
                });
            });

Upvotes: 1

Views: 7459

Answers (3)

Aditya
Aditya

Reputation: 4463

Here is the Working Fiddle :

Keep it simple :

$('#myTable1 tr').each(function() {
       var columns = $(this).find('td');

       columns.each(function() {
           var box = $(this).find('input:checkbox');

           if(box.is(":checked") && box.attr("changed") == 'yes')
           {
                myArray.push(box.attr('checkboxtype'));
                filepath = box.attr('path');                            
            }
        });
    });
         console.log(myArray);
  });

Upvotes: 1

Naveen Chandra Tiwari
Naveen Chandra Tiwari

Reputation: 5123

try this :-

     $('#myTable1 tr').each(function () {
                myArray = [];
                $(this).find("td input:checkbox").each(function () {
                    if ($(this).is(":checked") && $(this).attr("changed") == 'yes') {
                        myArray.push($(this).attr('checkboxtype'));
                        filepath = $(this).attr('path');
                        console.log(myArray);
                        console.log(filepath);
                    }
                });
            });

Upvotes: 0

Scimonster
Scimonster

Reputation: 33409

You should be using $("input[type='checkbox']:checked").closest("td").siblings("td").each().

See the difference between $().each() and $.each().

Upvotes: 0

Related Questions