Is there a way to add a "dynamic" data-attribute to a DOM element with jQuery?
This, for example, doesn't work:
var value = 100;
// hoping to add a 'data-100' attribute
$('div').attr({
String('data-' + value) :'display: block'
});
It throws the error:
Uncaught SyntaxError: Unexpected token (
obs: as far as I know if you sum a string with an integer in JS, it will cast everthing as a String, but anyway I tried with String() because maybe in some language that worked for me once.
This obviously won't work (it will add the 'custom_data' and not the 'data-100' attribute):
var custom_data = 'data-100';
// hoping to add a 'data-100' attribute
$('div').attr({
custom_data :'display: block'
});
String()would be valid ?$('div').attr('data-'+value, 'display: block');