Pardeep Pardeep - 4 months ago 10
Ajax Question

Change html button content on success

I just want to toggle html button content(activate or deactivate) on click of button...now if button content is activate it changes this to deactivate....but not vice-versa... please have look on ajax code:

<button type="button" class="btn btn-success btn-sm" id="<?php echo $row->id; ?>" onclick="savestatus<?php echo $row->id; ?>(<?php echo $row->id; ?>)">Activate</button>


and my script is

<script>
function savestatus<?php echo $row->id; ?>(row_id){
$.ajax({
type: "POST",
url: "<?php echo base_url().'home/activate_buyer/'. $row->id; ?>",
data:{},
success:function( data )
{
$("#"+row_id).html("Deactivate");
}
});
}

</script>


please help...

Answer

Use text method to change the text of the button

  $("#"+row_id).text("Deactivate");

EDIT

i want if text is activate then onclick it becomes deactivate and vice-versa

$('.button').click(function(event){
$(this).text().trim()==="Active"?$(this).text("Deactive"):$(this).text("Active")
})

JSFIDDLE