1

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

I'm Geeker
  • 4,601
  • 5
  • 22
  • 41
Pardeep
  • 81
  • 1
  • 2
  • 11

5 Answers5

3

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
    }
});
Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32
1
$.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> 
Hmache
  • 140
  • 1
  • 6
0

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>
Dhara Parmar
  • 8,021
  • 1
  • 16
  • 27
0

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");   
    }
Dave
  • 3,073
  • 7
  • 20
  • 33
0

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

  $.ajax({
    type: "POST",
    url: "<?php echo base_url().'home/accept_seller/'. $rrr->id; ?>",
    data:{},       
    success:function( data )
    {
       $("#buttonid").html('Changetext');
    }
   });
  • Hi, welcome to Stack Overflow. When answering a question that already has many answers, please be sure to add some additional insight into why the response you're providing is substantive and not simply echoing what's already been vetted by the original poster. This is especially important in "code-only" answers such as the one you've provided. – chb Apr 17 '19 at 13:42