0

I have dynamic html table and every cell have one checkbox. I want to get the selected checkbox if the user select from multiple checkbox from different row.

function GetAllChecked() {
             var chkedshid = new Array();
             var rows = new Array();
             rows = document.getElementById("Tbl_Id").getElementsByTagName("tr");
             trcount = rows.length;

             for (var i = 0; i < rows.length; i++) {
                 trid = rows[i].id;
                 chkedshid = chkedshid + chkedshid
                 if (inputList = document.getElementById(trid).getElementsByTagName("input")) {
                     for (var n = 0; n < inputList.length; n++) {
                         if (inputList[n].type == "checkbox") {
                             if (inputList[n].checked == true) {
                                 chkedshid[n] = inputList[n].id;

                             }
                         }
                     }
                 }
  }
             document.getElementById('Hidden_CellSelected').value = chkedshid.join();
             document.getElementById("BtnSav2Cart").click();
         }

why why this function return just last selected checkbox for last row in loop???? i need the all selected checkbox for all rows!!!!!!!

0

4 Answers 4

2

Assuming you are using jQuery.

Then you can simply do -

$("#myTableId input:checked")

If your checkbox have specific class then you can also do -

$("#myTableId .specificCheckboxClass:checked")
Sign up to request clarification or add additional context in comments.

Comments

0

On some Button click try to execute this code

var checkedTransactions = $("input:[name='idofcheckboxe']:checked").map(function () { return $(this).val(); }).get();

var will return all id of check boxes which are selected

2 Comments

how can i use it by using loop because i don't know the id for checkbox? so i need to do loop for every cell in every row and get every element checkbox id!!!!
no need of ID. if you are having class you can use that also var checkedTransactions = $("input:[class='classname']:checked").map(function () { return $(this).val(); }).get(); If this info is useful for you please vote for this answer
0

thanks all i solve the problem:

           function GetAllChecked() {
             var chkedshid = new Array();
             var rows = new Array();
             rows = document.getElementById("Tbl_Id").getElementsByTagName("tr");
             trcount = rows.length;

                  var totlchk = new Array();    
             for (var i = 0; i < rows.length; i++) {
                 trid = rows[i].id;


                 if (inputList = document.getElementById(trid).getElementsByTagName("input")) {
                     for (var n = 0; n < inputList.length; n++) {
                         if (inputList[n].type == "checkbox") {
                             if (inputList[n].checked == true) {
                                 chkedshid[n] = inputList[n].id;
                             }
                         }
                     }
                     totlchk = totlchk.concat(chkedshid.join());
                 }

             }

             document.getElementById('myHiddenfield').value = totlchk.join();
             document.getElementById("BtnSav2Cart").click();
         }

Comments

0
var table = document.getElementById("mytable");
checkbox = table.getElementsByTagName("input"); 

for(var indexCheckbox = 1; indexCheckbox<checkbox.length; indexCheckbox++){
    if(checkbox[indexCheckbox].checked){

        //do something
    }   
}

1 Comment

Please add some explanation to your answer!

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.