0

is there any attribute available in html table td where i can save my data and get it easily using jquery. Like suppose i have

<table>
  <tr>
    <td>Cell A</td>
    <td>Cell B</td>
  </tr>
</table>

From above snippet, I can get Cell value through using html function of jquery but i want something like this

<table>
      <tr>
        <td dataSaveHere>Cell A</td>
        <td dataSaveHere>Cell B</td>
      </tr>
    </table>

That i store some values in td so that later i can access it easily

Muhammad Faizan Khan
  • 10,013
  • 18
  • 97
  • 186

3 Answers3

7

Yes, data-* prefixed custom attribute to persist arbitrary data which can be fetched using .data()

It also has native support, You can also use Element.dataset property

console.log($('td').data('id'));
console.log(document.querySelector('td').dataset.id);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td data-id="1">Cell A</td>
  </tr>
</table>
Satpal
  • 132,252
  • 13
  • 159
  • 168
1

You can by using jQuery data.

<td data-name="dataSaveHere">Cell A</td>

and access it using jQuery.

$('td').data('name');
Vandolph Reyes
  • 622
  • 6
  • 18
0

you can create any attribute in html element like

$("your element").attr("attrname","your value")

if you want to read value

$("your element").attr("attrname")
Dhaval Pankhaniya
  • 1,996
  • 1
  • 15
  • 26
  • 1
    Creating random attributes is generally not recommended and it's better to use `data-*` attributes. Various reasons, mostly that it's not "valid" html. A limited search came up with this for more info: http://stackoverflow.com/a/926040/2181514 – freedomn-m May 05 '17 at 12:48