JS
var menuBtn = document.getElementsByClassName('btn-block');
var lenOfMenuBtn = menuBtn.length;
for (var i = 0; i < lenOfMenuBtn; i++) {
menuBtn[i].addEventListener('click',addMenu,false);
}
function addMenu() {
addOkFlag();
//addToCart();
//removeOkFlag();
}
function addOkFlag() {
var elmnt = document.createElement('i');
elmnt.setAttribute('class','glyphicon glyphicon-ok');
this.appendChild(elmnt);
}
Error is "this.appendChild is not a function". I want to call multiple function when user click the button so I call first function where I want to add <i>
tag in it but I don't how to pass current node from addEventListener to append this child.
You can pass the clicked element to the function
var menuBtn = document.getElementsByClassName('btn-block') ;
var lenOfMenuBtn = menuBtn.length ;
for (var i = 0; i < lenOfMenuBtn; i++) {
menuBtn[i].addEventListener('click',addMenu,false) ;
}
function addMenu(e) {
addOkFlag(e.target) ;
// or addOkFlag(this);
}
function addOkFlag(el) {
var elmnt = document.createElement('i');
elmnt.setAttribute('class','glyphicon glyphicon-ok');
el.appendChild(elmnt);
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments