JS - Assign hidden value to cell
-
11-06-2021 - |
Domanda
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.
Soluzione
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;
Altri suggerimenti
You can attach an attribute named 'data-' fowlloed by anything to any DOM node. Use the standard attribute access and change functions to read and write the value which will of course be a string.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow