The function jquery.data
does not pass the data-
attribute it just does the recording in the "memory".
In case you can create a function using jQuery.fn.extend
, like this:
jQuery.fn.extend({
"dataAttr": function(name, value) {
if (typeof value === "undefined") {
return $(this).attr("data-" + name);
}
return this.each(function() {
$(this).attr("data-" + name, value);
});
});
$( document ).on( "click", ".box_campo ul li", function() {
$(this).prev('input').dataAttr('ATRIBUTO', valor);
});
Note: jquery.data
works with the data-
attributes, as this answer .
Alternative
If you only need to write the data to the elements and retrieve them only, you can use jquery.data
, without need attributes.
Example usage:
$(this).prev('input').data( "foo", 52 );
$(this).prev('input').data( "bar", { myType: "test", count: 40 });
$(this).prev('input').data( { baz: [ 1, 2, 3 ] });
$(this).prev('input').data( "foo" ); // 52
Your code should be something like:
$( document ).on( "click", ".box_campo ul li", function() {
$(this).prev('input').data('Sua chave', valor);
});
To test:
$( document ).on( "click", ".box_campo ul li", function() {
alert( $(this).prev('input').data('teste') );//Retorna undefined
$(this).prev('input').data('teste', 'valor');
alert( $(this).prev('input').data('teste') );//Retorna valor
alert( $(this).prev('input').attr('data-teste') );//Provavelmente retorna undefined
});