Skip to content
Advertisement

Change button value on ajax success

I want to be able to change the value of the button from “Submit” to “Submitted” when ajax succeeds.

success: function () {
    Swal.fire({
      title: "Message recieved!",
      text: "We will get back to you soon.",
      icon: "success",
    }); 
    $("#contactFormButton").click(function(){
      // disabling the button
      $(this).prop("disabled",true);
      // Adding a mail submitted icon 
      $(this).html(
        'Submitted! <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-envelope-check-fill" viewBox="0 0 16 16"><path d="M.05 3.555A2 2 0 0 1 2 2h12a2 2 0 0 1 1.95 1.555L8 8.414.05 3.555ZM0 4.697v7.104l5.803-3.558L0 4.697ZM6.761 8.83l-6.57 4.026A2 2 0 0 0 2 14h6.256A4.493 4.493 0 0 1 8 12.5a4.49 4.49 0 0 1 1.606-3.446l-.367-.225L8 9.586l-1.239-.757ZM16 4.697v4.974A4.491 4.491 0 0 0 12.5 8a4.49 4.49 0 0 0-1.965.45l-.338-.207L16 4.697Z"/><path d="M16 12.5a3.5 3.5 0 1 1-7 0 3.5 3.5 0 0 1 7 0Zm-1.993-1.679a.5.5 0 0 0-.686.172l-1.17 1.95-.547-.547a.5.5 0 0 0-.708.708l.774.773a.75.75 0 0 0 1.174-.144l1.335-2.226a.5.5 0 0 0-.172-.686Z"/></svg>'
      );
    });
}

This works np. But I want it to work when the button is not clicked…In other words the button value “submit” should get changed to “Submitted” when ajax succeeds without the need to click the button again. Any help would be greatly appreciated.Thank you

Advertisement

Answer

For anyone wondering, I couldn’t get $('#ID').html('Submitted') to work.
Instead the following worked np:

success: function () {
    Swal.fire({
      title: "Message recieved!",
      text: "We will get back to you soon.",
      icon: "success",
    }); 
    //change button text to submitted
    $(document.getElementById('contactFormButton')).html('Submitted! <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-envelope-check-fill" viewBox="0 0 16 16"><path d="M.05 3.555A2 2 0 0 1 2 2h12a2 2 0 0 1 1.95 1.555L8 8.414.05 3.555ZM0 4.697v7.104l5.803-3.558L0 4.697ZM6.761 8.83l-6.57 4.026A2 2 0 0 0 2 14h6.256A4.493 4.493 0 0 1 8 12.5a4.49 4.49 0 0 1 1.606-3.446l-.367-.225L8 9.586l-1.239-.757ZM16 4.697v4.974A4.491 4.491 0 0 0 12.5 8a4.49 4.49 0 0 0-1.965.45l-.338-.207L16 4.697Z"/><path d="M16 12.5a3.5 3.5 0 1 1-7 0 3.5 3.5 0 0 1 7 0Zm-1.993-1.679a.5.5 0 0 0-.686.172l-1.17 1.95-.547-.547a.5.5 0 0 0-.708.708l.774.773a.75.75 0 0 0 1.174-.144l1.335-2.226a.5.5 0 0 0-.172-.686Z"/></svg>');
    //disable button
    $(document.getElementById('contactFormButton')).prop('disabled',true);
},
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement