Aquí tienes un ejemplo de código mas simplificado const modals = document.querySelectorAll('.Modal-Container') const buttons = { accept: { name: 'accept', number: 'Button1' }, cancel: { name: 'cancel', number: 'Button2' }, custom: { name: 'custom', number: 'Button3' }, action: function (params) { alert(params) } } modals.forEach(e => { Object.keys(buttons).forEach(key => { let btn_node let name = buttons[key].name let number = buttons[key].number if (name !== 'action') { let btn = document.createElement('BUTTON') btn.classList.add('Btn') btn.classList.add(name) btn_node = document.createTextNode(`${e.id} ${number}`) btn.onclick = function () { buttons.action(`${e.id} ${number}`) } btn.appendChild(btn_node) e.querySelector('.Modal-Footer').appendChild(btn) } }) })