JQuery attribute value for custom attributes

I need to get the attribute value, but it says it is undefined.

$(document).ready(function() { 
    $(".try").click(function() {
        alert($(this).attr("value"));
    });
});
<table id="try" class="try" cellspacing="0" cellpadding="0">
<td style="background:#80FF80;" value="WD">WD</td>
<td style="background:#FFFF80;" value="RD">RD</td>
<td style="background:#879FDE;" valu ="OD">OD</td>
</tr>
</table>
Run codeHide result
+4
source share
1 answer

You must include the child in your selector. Also your html is incorrect. You lack open tr:

$(".try tr td").click(function() {
  alert($(this).attr("value"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="try" class="try" cellspacing="0" cellpadding="0">
  <tr>
    <td style="background:#80FF80;" value="WD">WD</td>
    <td style="background:#FFFF80;" value="RD">RD</td>
    <td style="background:#879FDE;" value="OD">OD</td>
  </tr>
</table>
Run codeHide result
+3
source

All Articles