I have the following HTML. I have provided it only to provide context, and it doesn't need to be reviewed in detail.
When clicking a a.displayDetail
which is dynamically generated, dialog2
pops up as expected. But for my actual application, it then proceeds to redirected to another page. Obviously, my actual application has some other event tied to the element.
How can I find all event handlers tied to a dynamically generated element?
I have read related posts such as the following, but they don't seem to apply to dynamically generated elements.
- Can I find events bound on an element with jQuery?
- jQuery find events handlers registered with an object
Thank you
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Testing</title>
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/ui-lightness/jquery-ui.css" type="text/css" rel="stylesheet" />
</head>
<body>
<button id="start">start</button>
<div id="dialog1">
<ol id="ol"></ol>
</div>
<div id="dialog2"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.js" type="text/javascript"></script>
<script src="clicks.js" type="text/javascript"></script>
</body>
</html>
clicks.js
$(function(){
$('#start').click(function(){$("#dialog1").dialog("open");});
$("#dialog1").dialog({
autoOpen : false,
modal : true,
open : function() {
$.get('ajax.php',function (json) {
var ol=$('#ol').empty();
for (var i = 0; i < json.length; i++) {
ol.append($('<li />',{'data-id': json[i].id, 'class':'myclass', html:'<p class="message">'+json[i].html+'</p>'}));
}
},'json');
}
});
$("#ol").on("click", "a.displayDetail", function(){$("#dialog2").dialog("open");});
$("#dialog2").dialog({
autoOpen : false,
modal : true,
open : function() {
$(this).html('<p>Hello</p>');
alert('pause');
}
});
});
ajax.php
<?php
header('Content-Type: application/json;');
$array=array(
array('id'=>1,'html'=>'first <a href="#" class="displayDetail">click me</a> bla bla bla'),
array('id'=>2,'html'=>'second <a href="#" class="displayDetail">click me</a> bla bla bla')
);
echo json_encode($array);
?>