JSFiddle - React, Tailwind, and code Playground
HTML
<div id="container" style="background-color:azure">
<p>This container has a button, which will forget its <code>click()</code>...</p>
<input id="testbutton" type="button" value="Click Me!"/>
</div>
<script type="text/javascript">
$(function(){
$("#testbutton").click(
function(){
alert("Button has been clicked!");
})
});
</script>
<div style="background-color:#99FFFF;margin-top:20px">
<p>... when this button reseats <code>html</code></p>
<input id="actionbutton" type="button" value="Toggle"/>
</div>
<script type="text/javascript">
var toggle = false;
var html;
$(function(){
$("#actionbutton").click(
function(){
toggle = !toggle;
if(toggle){
html = $("#container").html();
$("#container").html("");
} else $("#container").html(html);
})
});
</script>
<div style="margin-top:50px">
<p>Without modifying anything in the second <code>script</code> tag, make it so that Toggle button restores the click functionality also. I think the problem is that the button's event handler is garbage collected. I have a form that undergoes a similar transition, and it's not possible to do <code>disply:none</code> or some such, because of other limitations. I think I need to dangle a reference to the function somewhere...</p>
<p style="font-style:italic;margin-top:10px;">Tested with latest Chrome and IE</p>
</div>