0

i had a loop which is used to create a table....my problem is to push the rows which are in the table to an array using javascript.i had tried but my output is coming in this way

[" cap ", " crocin", "123"] 

[" cap ", " crocin", "123", " choclate ", " silk", "123"] 

i want the output in this way

[" cap ", " crocin", "123"] 

[" choclate ", " silk", "123"]

this is my code

function addProducts(){

    array=[];
    var checkBox=document.getElementsByName("check");
    console.log(checkBox.length);
    var content='';
    for(i=0;i<checkBox.length;i++){
        if(checkBox[i].checked)
        {
            console.log(checkBox[i].parentNode.parentNode.getElementsByTagName("td")[0].textContent); 
            content += '<tr class="sales_details"><td>'+checkBox[i].parentNode.parentNode.getElementsByTagName("td")[0].textContent+'</td><td>'+checkBox[i].parentNode.parentNode.getElementsByTagName("td")[1].textContent+'</td><td>'+checkBox[i].parentNode.parentNode.getElementsByTagName("td")[2].textContent+'</td>
</tr>';

        }
    }
    document.getElementById("selectProduct").innerHTML = content;
}

array1=[];

function sales_det(){

    var salDet=document.getElementsByClassName("sales_details");
    //console.log(salDet.length);
    for (var i = 0; i < salDet.length; i++) {
        var sal=salDet[i].getElementsByTagName("td");
        array1.push(sal[0].textContent,sal[1].textContent,sal[2].textContent,sal[3].textContent);

        console.log(array1);
    }

}  

2 Answers 2

1

I think you will have to use window to create the new arrays , then push new values:

function sales_det(){

        var salDet=document.getElementsByClassName("sales_details");
        //console.log(salDet.length);
        for (var i = 0; i < salDet.length; i++) {
            var sal=salDet[i].getElementsByTagName("td");
            window["array_"+i] = [];
 window["array_"+i].push(sal[0].textContent,sal[1].textContent,sal[2].textContent,sal[3].textContent);

           console.log(window["array_"+i]);
        }

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

Comments

1

You need to empty the array each time through the loop

function sales_det(){
    var salDet=document.getElementsByClassName("sales_details");
    //console.log(salDet.length);
    for (var i = 0; i < salDet.length; i++) {
        var array1 = [];
        var sal=salDet[i].getElementsByTagName("td");
        array1.push(sal[0].textContent, sal[1].textContent, sal[2].textContent, sal[3].textContent);

        console.log(array1);
    }

}  

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.