You correctly assign and read data attributes using jQuery's .data() as follows:
// Assign a data attribute
$(selector).data('my-attr', 'test value');
// Read a data attribute
var value = $(selector).data('my-attr');
If you want the data attributes added to the DOM itself, you would use jQuery's attr() as follows:
// Assign a data attribute - note the need to prefix it with "data-"
$(selector).attr('data-my-attr', 'test value');
// Read a data attribute - note the need to prefix it with "data-"
var value = $(selector).attr('data-my-attr');
Examples:
$(function() {
// This value is stored in the jQuery cache object
$(".testclass").data('val', 'something1');
alert($(".testclass").data('val'));
// Alternatively, this value is stored on the DOM element itself
$(".testclass").attr('data-val', 'something2');
alert($(".testclass").attr('data-val'));
});
Fiddle: http://jsfiddle.net/BenjaminRay/hau9gac9/