1

I am working on this demo. How can I push values of checked rows in JS Array or Object?

var obj=[
    {
        id : "001",
        name : "apple",
        category : "fruit",
        color : "red"
    },
    {
        id : "002",
        name : "melon",
        category : "fruit",
        color : "green"
    },
    {
        id : "003",
        name : "banana",
        category : "fruit",
        color : "yellow"
    }
]
$.each(obj, function (index, item) {
     var eachrow = "<tr>"
                 + "<td id="+index+"><input type='checkbox'></td>"
                 + "<td>" + item["id"] + "</td>"
                 + "<td>" + item["name"] + "</td>"
                 + "<td>" + item["category"] + "</td>"
                 + "<td>" + item["color"] + "</td>"
                 + "</tr>";
     $('#tbody').append(eachrow);
});

2 Answers 2

1

Try this one,

Live demo

You will get desired output once you click get rows button.

$("#btn").on("click",function(){

    var checkedRows = [];
    $("#tbody tr").each(function(){

        if($(this).find("input").is(":checked")){
            checkedRows.push($(this).find("td:eq(1)").html());
        }    
        console.log(checkedRows);    
    });

    var result = [];

    $.each(obj,function(item){
        if(checkedRows.indexOf(item.id)>-1)
       result.push(item) ;
    });   

      console.log(result);    

});
Sign up to request clarification or add additional context in comments.

Comments

0

If I'm understanding you correctly, you could try something like this:

$(function(){
  $('input[type="checkbox"]').on('change', function(){
    data.push($(this).parent().attr('id'));
  });
});

Updated fiddle here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.