index.php:
<html>
<head>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<div id="box"></div>
<div onclick="getContent()">Open Window</div>
</body>
</html>
script.js:
function getContent() {
//ajax call that returns the html from content.php and places it in the box div
}
function sayHello() {
console.log('Hello');
}
function sayGoodBye() {
console.log('Good Bye');
}
content.php:
<div onclick="sayHello()">Say Hello</div>
<div onclick="sayGoodBye()">Say Good Bye</div>
What is the correct way to make the functions sayHello() and sayGoodBye() work?
Currently they do nothing.
As your element comes from ajax and append to
#boxdynamically, so you need an delegate event handling.Syntax for delegate of
.on()is:To trigger you need to use
NOTE
I think it should work normally
Look here
And its also possible to trigger those click events
Check this