data-*
are customizable attributes of HTML elements, it is stored as a string property in the element’s dataset object. If you’re saving a list of values in it, you’re actually concatenating string.
For example:
var num = 0; // a fins de exemplo
$('#meu_botao').data('order', $('#meu_botao').data('order') + ', day-' + num++);
This can be added to the click event that will result in adding values to the attribute data-order
, but it’ll stay that way:
'day-1, day-2, day-3' // e por ai vai, conforme for sendo adicionado
You can break this string later in an Array to handle the values for example:
$('#meu_botao').data('order').split(', '); // ["day-1", "day-2", "day-3"]
EDIT (after better explanation of the question):
In case you want to increment the value of day-{n} contained in date, you can use the String.prototype.match
to take the existing value.
var num = $('#meu_botao').data('order').match(/day-([0-9]*)/), num = num && + num[1] || 0;
$('#meu_botao').data('order', 'day-' + num++);
This way you will not need to cache the number in a variable and will always be able to use the value contained in data-*
Exactly what I was looking for man, vlw! o/
– Dennis Braga
If possible, create an example using the tool
trecho de código
here on Sopt. So, if for some reason there is a problem with the code posted on Jsfiddle, the question will be useful.– Renan Gomes