How do browsers handle invalid / unspecified attributes on HTML elements?
4 answers
Browsers will not complain about unrecognized attributes, and Javascript and jQuery will still be able to access them:
console.log( $('#blah').attr('myattribute') ); // something console.log( document.getElementById('blah').getAttribute('myattribute') ); // something However, you must use the HTML5 data-* attribute, which is specifically designed for custom attributes. jQuery has a data() method to access / set:
<div id="blah" data-myattribute="something">whatever</div> <script> console.log( $('#blah').data('myattribute') ); // something </script> +3