user3013365 user3013365 - 2 months ago 9
HTML Question

Getting data attribute value in td

I am adding data attribute in td something like this :

<td style="text-align:center;padding: 3px 0;data-id:2;data-env:PL1;"><img src="/VendorFeedDevUI/Content/green.png" style="width:25px" alt="Green"><div>21 Dec 14<br>23:55:00</div></td>

When I want to retrieve the same attribute in Jquery I am using below code :

$(".gradienttable tr td").click(function () {
var id = $(this).attr('data-id');
var env = $(this).data('env')

I am trying to use both ways to get data attributes but both are returning undefined.
Also I am getting td object for
so that is not an issue.
So what am I doing wrong ?


You are using it the wrong way:

 <td style="text-align:center;padding: 3px 0;" data-id="2" data-env="PL1">...</td>

You add the data attributes to your styles, you have to place it outside the style attribute.