I am trying to insert a new image to an existing element. I have
var backImg = createElement('img', { className : 'link', src : '/images/btn.png' });
var save_bt=document.getElementsByClassName('button');
save_bt.appendChild(backImg);
The above codes gave me error:
Object #<NodeList> has no method 'appendChild'
Can anyone help me about it? No Jquery codes plz. Thanks a lot!
document.getElementsByClassName('button')returns aNodeList, not an element. If you want to addbackImgto every element with the class.buttonyou would need to loop through that NodeList:If you are trying to target a single element, you probably want to use
idHTML attribute instead ofclass, then:Note that HTML ids must be unique within the document.