0

How to run JavaScript from buttons onClick in an AJAX response?

I've a HTML div element which is loaded from an ajax. Within this div there is an <button> with an attribute onClick defined to run a javascript function. The full response looks like this:

<div class="Stuff">
<p>FooBar</p><button onclick="openMenu('Hello')">open</button>
</div>

<script type="text/javascript">
 function openMenu(name) {
    console.log(" You clicked: ", name);
    }
</script>

So, how to execute the sript on button click? In the console is says:

ReferenceError: openMenu is not defined

The div and the script get inserted via an XMLHttpRequest and document.getElementById("content-row").innerHTML = this.responseText where "content-row" is a div as well.

Any help appreciated.

Jim Knopf
  • 59
  • 7

0 Answers0