I have written a jQuery code in the admin section of my site that turns the site offline without page refresh. It works great, but for one thing:
When I click on TURN ON the ajax call is made accurately and all the classes are changed and the text is changed the TURN OFF. However, when I click on this text, nothing happens. If I reload the page, then I will be able to.
$("#switch_off").click(function() {
$("#switch_off").html("<img style=\"padding-left:15px;\" src=\"/img/admin/ajax-loader.gif\">");
$('#switch_off').removeClass('ttip_b')
$.ajax({
url: "/settings/SwitchOffline",
type: "get",
data: '',
success: function(responseText, statusText, xhr, $form){ // Trigger when request was successful
$("#site_status").html("<span id=\"offline\" class=\"lbl error_bg\">Offline</span><span id=\"switch_on\" class=\"ttip_b\" title=\"Click to place Site Online\">Turn On</span>");
},
error: function(responseText){
//alert(responseText);
}
});
return false;
});
$("#switch_on").click(function() {
$("#switch_on").html("<img style=\"padding-left:15px;\" src=\"/img/admin/ajax-loader.gif\">");
$('#switch_on').removeClass('ttip_b')
$.ajax({
url: "/settings/SwitchOnline",
type: "get",
data: '',
success: function(responseText, statusText, xhr, $form){
$("#site_status").html("<span id=\"online\" class=\"lbl ok_bg\">Online</span><span id=\"switch_off\" class=\"ttip_b\" title=\"Click to place Site Offline\">Turn Off</span>");
},
error: function(responseText){
//alert(responseText);
}
});
return false;
});


HTML CODE WITH SOME PHP
<div class="user_info user_sep" style="width:90px;">
<p class="sepH_a">
<strong>Site Status</strong>
</p>
<?php
//debug($site_offline);
if($site_offline){
?>
<span id="site_status">
<span id="offline" class="lbl error_bg">Offline</span>
<span id="switch_on" class="ttip_b" title="Click to place Site Online">Turn On</span>
</span>
<?php }else{ ?>
<span id="site_status">
<span id="online" class="lbl ok_bg">Online</span>
<span id="switch_off" class="ttip_b" title="Click to place Site Offline">Turn Off</span>
</span>
<?php } ?>
</div>
Try using event delegation instead because you are rewriting the HTML on every click and the bindings are being lost. By making use of event delegation you are binding the event for present elements in the DOM and future ones that will be appended to the DOM such as those ones resulting from your AJAX calls.
Use
instead of
If
#site_statusis also being overwritten then usedocument.