I am trying to add onclick properties to multiple buttons using DOM. langButton.onclick = searchEngine(langButton);
does not add the property to the button and eventListener does not work either. I need to read the text of the specific button that is clicked, or just copy the button all together. Here is the code block:
for (j = 0; j < applications[i].languages.length; j++) {
let langButton = document.createElement('button');
langButton.classList.add('btn-secondary');
langButton.innerHTML = `${applications[i].languages[j]}`;
langButton.onclick = searchEngine(langButton);
// console.log(langButton.innerHTML);
langDiv.appendChild(langButton);
}
btn-secondary is a Bootstrap class.
function searchEngine(button) {
return button
}
returns the button, but runs each time it tries to set the onclick property. Then the buttons do not return anything after being clicked. The console.log(langButton.innerHTML)
returns the button's text just fine.