Repeating jQuery Ajax success function

The Bobster

I'm using Ajax to add an array of data to a database.

Currently when "#bookingbutton" is clicked on this returns a block of HTML with ".select-room-button" button.

I've added the ".select-room-button" code in the success function of the original Ajax call for "#bookingbutton" otherwise it doesn't work.

I want to be able to click on the ".select-room-button" unlimited times without having to repeat the code loads of times in each success function if that makes sense? I feel like there must be a smarter way to do this but I'm not sure how?

jQuery(document).ready(function($) {

$('#bookingbutton').click(function() {

    $('.booking-main').html('<img src="http://mailgun.github.io/validator-demo/loading.gif" alt="Loading...">');
    $('.booking-side-response').html('<img src="http://mailgun.github.io/validator-demo/loading.gif" alt="Loading...">');

    $.ajax({
        type: 'POST',
        url: AJAX_URL,
        data: $('#bookroom').serialize(),
        dataType: 'json',
        success: function(response) {

            if (response.status == 'success') {
                $('#bookroom')[0].reset();
            }

            $('.booking-main').html(response.content);
            $('.booking-side-response').html(response.sidebar);


            $('.select-room-button').click(function() {

                $('.booking-main').html('<img src="http://mailgun.github.io/validator-demo/loading.gif" alt="Loading...">');
                $('.booking-side-response').html('<img src="http://mailgun.github.io/validator-demo/loading.gif" alt="Loading...">');

                $.ajax({
                    type: 'POST',
                    url: AJAX_URL,
                    data: $('#bookroom').serialize(),
                    dataType: 'json',
                    success: function(response) {
                        if (response.status == 'success') {
                            $('#bookroom')[0].reset();
                        }

                        $('.booking-main').html(response.content);
                        $('.booking-side-response').html(response.sidebar);

                    }
                });

            });

        }
    });

});

});

amitguptageek

For this you can use various selector for a single event to trigger ajax calls. So your code snippet will look like

jQuery(document).ready(function($) {

$(document).on("click",'#bookingbutton, .select-room-button', function() {

$('.booking-main').html('<img src="http://mailgun.github.io/validator-demo/loading.gif" alt="Loading...">');
$('.booking-side-response').html('<img src="http://mailgun.github.io/validator-demo/loading.gif" alt="Loading...">');

$.ajax({
    type: 'POST',
    url: AJAX_URL,
    data: $('#bookroom').serialize(),
    dataType: 'json',
    success: function(response) {

              if (response.status == 'success') {
                   $('#bookroom')[0].reset();
              }

              $('.booking-main').html(response.content);
              $('.booking-side-response').html(response.sidebar);

        }
      });

    });
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Jquery Pjax - Ajax success function

From Java

Pass variable to function in jquery AJAX success callback

From Dev

jQuery/Ajax success function not rendering html

From Dev

jQuery: using $(this) inside of $.ajax success function

From Dev

Including Success / Fail to jQuery / Ajax function

From Dev

jQuery AJAX return the function as data upon success

From Dev

reading a string table jquery ajax success function

From Dev

Jquery ajax call inside success function

From Dev

Ajax using JQuery success function is not called

From Dev

Ajax success and error function is not calling properly in jquery

From Dev

Set a delay in a repeating jQuery / Ajax function

From Dev

ajax success function not redirecting on success

From Dev

ajax call inside ajax success jquery then ajaxstart function not working

From Dev

jquery appendTo function not working inside ajax success function

From Dev

Ajax success function

From Dev

ajax success function not firing

From Dev

AJAX success function not executing

From Dev

Manipulate AJAX function on success

From Dev

AJAX not running success function

From Dev

AJAX success function not loading

From Dev

$.ajax() Success Function Not Working

From Dev

$.ajax success function wait

From Dev

JQuery bind on Ajax success

From Dev

parameters in jquery ajax success

From Dev

JQuery ajax success scope

From Dev

Bind dropdownlist from jquery ajax call success function using angularjs

From Dev

jQuery .ajax calls success function before sending request

From Dev

Is there a way to pass variables from beforeSend to success functions in the jQuery .ajax function?

From Dev

Calling function inside jQuery ajax success response works once but not twice

Related Related

HotTag

Archive