0

So I figured I would try to add the 'value' attribute to a TD tag because I need to store a value in a table and really kind of wanted it to be not so obvious, to my amazement, using jQuery I was able to retrieve this value.

My question is why am I able to get this value when it isn't a valid attribute and since I can, would it be safe to use.

HTML

<table id="tblTest">
  <tr>
    <td value="0">Value is zero</td>
  </tr>
  <tr>
    <td value="1">Value is one</td>
 </tr>
</table>

Javascript:

$('#tblTest').on('click','tr', function(){
  alert($(this).children(':first').attr('value')); 
});

I have created a fiddle for it http://jsfiddle.net/r2Lqp/

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • [HTML5 provides data attributes](http://html5doctor.com/html5-custom-data-attributes/) for this kind of purpose. But yes, as far as I know, [all browsers let you do this](http://stackoverflow.com/questions/2412947/do-html5-custom-data-attributes-work-in-ie-6) -- but bear in mind that your HTML won't validate and there's no guarantee it'll work in all browsers in the future. The data attribute is more standard these days, validates, and is likely to be supported long into the future, with increasing additional support in browsers. – Matt Gibson Mar 22 '14 at 23:33
  • you can use for eg data-value attribute – rjanjic Mar 22 '14 at 23:35
  • Well, that's exactly what I wanted to know. :) Thanks! – Billy Hallman Mar 22 '14 at 23:43

1 Answers1

1

If I understand the question, my answer is this: you can add any attributes for personal use if they are not reserved in standards HTML.

user3141004
  • 115
  • 4