Wednesday, September 11, 2013

HTML 5 data- attributes

Sometimes, when programming in Javascript you need some data associated to HTML elements. Since the introduction of HTML5 we can use the data- custom attributes. With this attributes you can associate data to HTML elements and access it via javascript:

<div data-code="jo8389">Hello</div>

Using JQuery it's really easy to access those attributes:
$("div[data-code=jo8389]").text("Good bye!"); //selector
$("div").attr("data-code","1777"); // changing the attribute