The function jquery.data
don’t arrow the attribute data-
it just writes to "memory".
In case you can create a function using jQuery.fn.extend
, thus:
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 attributes data-
, in accordance with this answer.
Alternative
If you just need to write the data into the element and recover it only, you can use the jquery.data
, no need for attributes.
Example of use:
$(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 must be something like:
$( document ).on( "click", ".box_campo ul li", function() {
$(this).prev('input').data('Sua chave', valor);
});
For testing:
$( 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
});
Where do you want to put what? You can give an example of which
data-
should go where? want to put another HTML or a variable?– Sergio
maybe this will help, http://stackoverflow.com/questions/12862423/regular-expression-in-data-attribute-jquery
– Guilherme Lautert