Search code examples
javascriptcelltablecell

JS - Assign hidden value to cell


Is there a way to assign a hidden value to a table cell? I would like to assign a certain value, an integer or a string to a table cell, so I can append and edit this value in javascript by using the id.


Solution

  • Try HTML5's data api. You can set an attribute on the cell:

    <td id="cell1" data-value="1">One</td><td id="cell2" data-value="2">Two</td>
    

    You can then use:

    document.getElementById('cell1').dataset.value;