Click here to Skip to main content
15,185,471 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I am facing a problem and I searching a lot of possible solutions, but no luck. So I hope to get help here. My case is showing some properties in an HTML table on the selected category. After the table(ID,Title,value) is shown, I have to take the properties from the table and send it to the controller. The problem is when looping inside the table the id gives me undefined. Any another suggestions to get the id?

What I have tried:

Here is my code:

JavaScript
AjaxCall('/Home/GetCategoryProperty', obj, 'POST').done(function (response) {

               if (response.length > 0) {

                   var DeviceVM = { "AvailableCatProperty": response }
                   console.log(DeviceVM);

                   $('#AvailableCatProperty').val(DeviceVM);

                   $('#catprop').html('');

                   table += '<table class="table table-bordered table-striped"
                   id=_catprop>';
                   table += '<thead class="thead-dark">';
                   table += '<tr><th>Property</th><th>Value</th></tr>';
                   table += '</thead>';
                   table += '<tbody>';

                   for (var i = 0; i < response.length; i++) {
                       table += '<tr data-id='+ response[i].id +'>';
                       table += '<td>' + response[i].title + '</td>';
                       table += '<td><input type="text" asp-for="' + response[i].value + '"/></td>';
                       table += '</tr>';
                   }
                   table += '</tbody></table>';
                   $('#catprop').append(table);

               }
               else {
                   $('#catprop').empty();
                   table = '<p> No Property For Selected Category !! </p>';
                   $('#catprop').append(table);
               }

To get the table data:

JavaScript
var table = document.getElementById("_catprop").rows;
for (let row of table) {
                for (let cell of row.cells) {
                    alert(cell.innerText + $("#id").val());
                }
            }
Posted
Updated 52 mins ago
v3

1 solution

Try the following code to get the table data:

var table = document.getElementById('_catprop');
for (i = 0; i < table.rows.length; i++){
   var tableCells = table.rows.item(i).cells;
   for(var j = 0; j < tableCells.length; j++){
		  var cellVal = tableCells.item(j).innerHTML; // Write your logic 
		  alert(cellVal);
	   }
}
   
Comments
Ramy82 22hrs ago
   
firstly thanks for your try , but it did not show the id's . want to tell you that the id is in the not like the title or the value .. hope to find the solution
M Imran Ansari 21hrs ago
   
I have created a fiddle to demonstrate how to get table ID data. Check the link:
https://jsfiddle.net/fqu059aL/
Ramy82 2hrs 15mins ago
   
great if the id is a hidden . I wrote it like this
https://jsfiddle.net/15wp68fo/6/
M Imran Ansari 2hrs ago
   
With hidden ID column also working as expected.
https://jsfiddle.net/ktbzmpwq/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900