i have created a button like this
var btn=document.createElement('BUTTON');
var t=document.createTextNode("X");
btn.appendChild(t);
x.appendChild(btn);
how can i add class to this button?
Not by using .appendChild()
, but rather by setting the .className
property on the created element itself:
btn.className = "myclass";
Or if you don't care about older browsers, you can use .classList
:
btn.classList.add("myClass");