The .data() method does not modify the DOM element's attribute. However, data is still saved via. $selector.data( key, value ); and can be retrieved via. $selector.data( key );
In order to add an attribute to a DOM element, you will likely want to use the .attr() method. With .attr(), an attribute can be attached to an element via. $selector.attr( key, value ); and can be retrieved via. $selector.attr( key );
validUsername(data) {
if (data === 'true') {
$("#usernameStatus").html("y").attr("data-valid", 1);
} else {
$("#usernameStatus").html("x").attr("data-valid", 0);
}
}
JSFiddle example
One of the advantages of using .attr() instead of .data() is the ability to find all elements with an attribute (and value) via. the $('[key="value"]') selector.