I am creating buttons in jQuery dynamically. I need to add data attributes:
So I saw the answer here: Dynamically create buttons with Jquery
$(document).ready(function() {
for(i = 1; i <=10; i++) {
$('<button/>', {
text: i, //set text 1 to 10
id: 'btn_'+i,
click: function () { alert('hi'); }
});
}
});
I am adding the data-attributes like this:
$(document).ready(function() {
for(i = 1; i <=10; i++) {
$('<button/>', {
text: i, //set text 1 to 10
id: 'btn_'+i,
click: function () { alert('hi'); }
}).attr('data-test', 1).attr('data-test2', 2);
}
});
I was thinking that if I could do something similar to the way we add text
and id
attributes