I'm trying to build a very simple VPS control panel; i have the following code that lists every VPS i have on a particular server :
($active variable is a BOOLEAN giving the current state of the VPS)
<ul id="vps">
<li <? if ($active): ?>class="active"<? endif;?> >
<? if ($active): ?>
<div class="status">running</div>
<?else:?>
<div class="status">halted</div>
<? endif; ?>
<div class="sub">
<div class="activity">
<? if ($active): ?>
<a href="#" id="<?=$vpsid?>" class="activevps"><img src="stop.png" /></a>
<? else: ?>
<a href="#" id="<?=$vpsid?>" class="desactivevps"><img src="start.png"/></a>
<? endif; ?>
</div>
</div>
And the following JQuery actions :
<script>
$('.activevps').click(function(e){
var target = $(this).attr('id');
$(this).html ('<img src="ajax-loader.gif" />');
var source = $(this);
var tt = new Date().getTime();
$.ajax({
type: "POST",
url: '/vpsadmin/stop/' + tt,
data: 'vps='+target,
dataType: "html",
timeout: (60 * 1000),
success: function(data) {
$('.vps_'+target).html ('<a href="#" id="'+target+'" class="desactivevps"><img src="play.png" /></a>');
},
error: function(objAJAXRequest, strError){alert(strError);
}
});
and same code for STOP action
<script>
$('.desactivevps').click(function(e){
var target = $(this).attr('id');
$(this).html ('<img src="ajax-loader.gif" />');
var source = $(this);
var tt = new Date().getTime();
$.ajax({
type: "POST",
url: '/vpsadmin/start/' + tt,
data: 'vps='+target,
dataType: "html",
timeout: (60 * 1000),
success: function(data) {
$('.vps_'+target).html ('<a href="#" id="'+target+'" class="activevps"><img src="stop.png" /></a>');
},
error: function(objAJAXRequest, strError){alert(strError);
}
});
I'm looking forward to do some actions here (but lost in jquery actions).
- When clicking on the start/stop icon; it should trigger the ajax post and :
- Change the 'li' class to 'inactive'
- Change the 'status' class to 'halted' or 'running'
- Change the 'a' class to activevps/desactivevps and also change the img src associated.
I'm quite new to Jquery; how to do all this using the simple click function ?