0

I want to print a value in each table cell after creating it dynamically.

<table id="MapDetails"><tr> <td/><td/><td/><td/> var colIndex = 4; foreach(MapDetail geMapDetail in Model.mapDetails) { <td class="test"> <script>{getPosition(@geResult.assessmentId, @colIndex, @rowIndex, '@geResult.ResultValue');}</script> </td> colIndex++; } </tr></table> 

My script

THIS DOES NOT WORK

function getPosition(id, colIndex, rowIndex, resultValue) { var element = '#' + id; var cell = $('#MapDetails tr:eq(' + rowIndex + ') td:eq(' + colIndex + ')'); if($(element).index() == colIndex){ cell.innerHTML = resultValue; } } 

THIS WORKS ONLY FOR THE FIRST CELL

function getPosition(id, colIndex, rowIndex, resultValue) { var element = '#' + id; var cell = $(".test").closest('tr').find('td').get(colIndex); if($(element).index() == colIndex){ cell.innerHTML = resultValue; } } 
5
  • have you give different id to all cells ? Commented Mar 16, 2017 at 5:09
  • @YasinPatel tds don't have any classname or id in the given markup. Commented Mar 16, 2017 at 5:10
  • creating it dynamically......How you are doing this? Commented Mar 16, 2017 at 5:11
  • td has a class name test in the markup i have given above <table id="MapDetails"><tr><td class="test"> Commented Mar 16, 2017 at 5:12
  • this td is in a forloop i am editing code to show..this is a very stripped down version of actual code to make my question clear Commented Mar 16, 2017 at 5:13

2 Answers 2

0

It is mostly a algorithm problem. You must loop through all TRs and nested TDs and write that one value that you want to write.

$('#tableid').find('tr').each(function(index, element){ $(element).find('td').each(function(indexd, elementd){ $(elementd).html('blah blah'); }); }); 
Sign up to request clarification or add additional context in comments.

2 Comments

what is meant by element here? in my code element is totally different from cell. also i am present inside the td itself when i call my getPosition function so traversing every time doesnt seem like a good idea...i have tried $(this).parent().children('td').get(colIndex) but even that doesnt work
please ask the question properly..I answered your question "I want to print a value in each table cell after creating it dynamically"
0

I found my answer here: How to set table cell value using jquery

so i did:

var cell = $("#MapDetails").children().children()[rowIndex].children[colIndex]; 

and that's it! Cheers

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.