I’ve got a button that displays “HI” when clicked. The button is in a div called “Panel1”, which I’m using AJAX to update. After clicking for the first time, when Panel1 is reloaded, the button’s click even does not fire anymore. I guess this has to do with event bubbling… but I just can’t seem to solve it for the last 3 days. Sample code is as follows:-
<script language="JavaScript" type="text/javascript">
document.observe("dom:loaded", function() {
$$("#ShowDialog").invoke('observe', 'click', function() {
alert("HI");
AjaxPanel.reload($("Panel1"));
});
</script>
</head>
<body>
<div id="Panel1">
.
.
.
<input name="btnAdd" type="submit" value="Add" id="ShowDialog">
</div>
</body>
</html>
Any advice is deeply appreciated.
You’re replacing the entire contents of
Panel1, including the button that the event listener is attached to.So when the window first loads, the event listener is correctly attached. But when the ajax request replaces the content of the div, the button disappears too. Even if the new content has a similar button, it (the new button) won’t have an event listener attached.
I don’t know the AjaxPanel “class” you’re using, but here’s how I’d do it with straight Prototype
Alternatively, you can just put the button outside the panel, so it doesn’t get replaced. Or you can use jQuery, and the
.live()method.