Pardeep
Pardeep

Reputation: 81

how to change button text after succes in ajax

I want to change button text on success.. I want to change this to accepted after success

    <button type="button" onclick="saveData<?php echo $row1->id; ?>()">Accept</button> 
      <script>
      function saveData<?php echo $rrr->id; ?>(){

      $.ajax({
        type: "POST",
        url: "<?php echo base_url().'home/accept_seller/'. $rrr->id; ?>",
        data:{},       
        success:function( data )
        {

        }
       });
  }

My function is properly working because i have used alert after success but i dontknow how to chage its text after success. pls help

Upvotes: 1

Views: 16339

Answers (5)

Munaf Wadiwala
Munaf Wadiwala

Reputation: 7

id; ?>()">Accept function saveDataid; ?>(){

  $.ajax({
    type: "POST",
    url: "<?php echo base_url().'home/accept_seller/'. $rrr->id; ?>",
    data:{},       
    success:function( data )
    {
       $("#buttonid").html('Changetext');
    }
   });

Upvotes: 0

Hmache
Hmache

Reputation: 130

$.ajax({
  type: "POST",
  url: "<?php echo base_url().'home/accept_seller/'. $rrr->id; ?>",
  data: {},
  success: function(data) {
   $("#btn").text('your new text here'); // add id to your button
  }
});

<button id="btn" type="button" onclick="saveData<?php echo $row1->id; ?>()">Accept</button> 

Upvotes: 1

Dave
Dave

Reputation: 3091

HTML : add id in button

<button type="button" id="accept" onclick="saveData<?php echo $row1->id; ?>()">Accept</button> 

script:

 success:function( data ) {
       $("#accept").html("accepted");   
    }

Upvotes: 0

Indrasis Datta
Indrasis Datta

Reputation: 8618

You can associate a class with the button:

<button type="button" onclick=".." class="submit-btn">Accept</button> 

Now, in your AJAX success code, mention the following:

 $.ajax({
    type: "POST",
    url: "<?php echo base_url().'home/accept_seller/'. $rrr->id; ?>",
    data:{},       
    success:function( data ) {
       $(".submit-btn").html("Accepted");   // Add this line
    }
});

Upvotes: 3

Dhara Parmar
Dhara Parmar

Reputation: 8101

1) pass this in function

2) Use .text() function to change text of button.

<button type="button" onclick="saveData<?php echo $row1->id; ?>(this)">Accept</button> 
<script>
          function saveData<?php echo $rrr->id; ?>(obj){
              $.ajax({
                 type: "POST",
                 url: "<?php echo base_url().'home/accept_seller/'. $rrr->id; ?>",
                 data:{},       
                 success:function( data )
                 {
                      $(obj).text("New Text");
                 }
              });
          }
</script>

Upvotes: 0

Related Questions