0

I am working on PHP ajax project. I want to disable and change the button's text after the success function call in ajax. my ajax function is working fine and I am getting the desired output. just one thing is remaining which is the button thing. Here is my code for ajax.

 $(document).ready(function () {

  $(document).on("click", "#add_btn", function (e) {
    e.preventDefault();
    var id = $(this).val();
    var cus_id = $('#customer_selection').val();
    if (!cus_id) {
      alert("Please select a customer")

    } else {

      $.ajax({
        url: "add_selection_script.php",
        type: "POST",
        data: {
          id: id,
          cus_id: cus_id
        },
        success: function (data) {
          // here i want to change button text and disable for click button only
            not others because my buttons are dynamic
          prop("disabled", true);
          html("Added");

        }
      });
    }
  });
});

here is the button code

<button type="submit" id="add_btn" value="<? echo $id; ?>">Add to selection</button>
1
  • FYI, I removed the PHP tag, since the question is all about JS. Commented Dec 23, 2022 at 11:06

2 Answers 2

1

you have to give identifier for which you have to make changes

$("#add_btn").prop('disabled', true);
$("#add_btn").html('Added');

if your button is dynamic, then you can do below: on click of button, get your element in a variable

var element = $(this);

and in your success function, write this

element.prop('disabled', true);
element.html('Added');
Sign up to request clarification or add additional context in comments.

1 Comment

Works perfect (y)
1

You need to set the attribute of the button and use .html or .text to change the text.

success: function (data) {
  $('#add_btn').attr('disabled', true);
  $('#add_btn').html('changed text');
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.