I would like to execute function in chrome extension (in popup.js) by clicking on button in a innerHTML.
My code in popup.html is:
<html>
<head>
<script src="popup.js"></script>
</head>
<body>
<div id="panier_container"> </div>
</body>
</html>
My code in popup.js :
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
if (msg.text === 'results') {
var panier_container = document.getElementById("panier_container");
var texte = "<button onclick=\"toto()\"> TOTO </button>";
panier_container.innerHTML = texte;
});
});
function toto() {
alert("toto");
}
When I execute the code, I see the button "TOTO" but when I click on the button, nothing happen. Out of chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) { the button execute the function. But inside no.
DOMContentLoaded). Most importantly, the edit did not invalidate the answer you already have, as that answer addressed the issue of trying to use an inlineonclickattribute, which is also what the 3rd duplicate addresses. However, please don't imply that we are wrong to have acted based upon the question and code as it existed at the time (i.e. prior to your edit).