skip to Main Content
  1. I have a php function, which generates calendar for the current
    month with button for next and previous months.
  2. I also have a js file, which monitors the click event and generates calendar for next or
    previous month.
  3. I am fetching the calendar data from the php function (as defined in step 1) using ajax function.

On clicking the button, I observe all the previous/next month calendar data in my console log but don’t know how to parse the data to update the current page with latest calendar data.

My js code

jQuery(document).ready(function(){ 
    jQuery('#prev_button_id').click(function(){
        jQuery.ajax({
            url: frontend_object.plugin_url_info + '/includes/previous_month.php',
            success: (response)=>{
                console.log(response);
            },
            error:(response)=>{
           console.log("prev month not loaded");
            }
        });
    });
});

My Console output for previous month calendar

 <table class='calendar'>
<div class = 'month_block'
><input id='prev_button_id' class='prev_button' type='button' value = '&#8249 Previous'/>
<div class = 'month'>April 2020</div>
<a class='next_button' href='TBD'>Next &#8250</a>
</div><tr><th class='header'>S</th><th class='header'>M</th><th class='header'>T</th><th class='header'>W</th><th class='header'>T</th><th class='header'>F</th><th class='header'>S</th></tr><tr><td colspan='3'>&nbsp;</td><td class='day' rel='2020-04-01'>1</td><td class='day' rel='2020-04-02'>2</td><td class='day' rel='2020-04-03'>3</td><td class='day' rel='2020-04-04'>4</td></tr><tr><td class='day' rel='2020-04-05'>5</td><td class='day' rel='2020-04-06'>6</td><td class='day' rel='2020-04-07'>7</td><td class='day' rel='2020-04-08'>8</td><td class='day' rel='2020-04-09'>9</td><td class='day' rel='2020-04-10'>10</td><td class='day' rel='2020-04-11'>11</td></tr><tr><td class='day' rel='2020-04-12'>12</td><td class='day' rel='2020-04-13'>13</td><td class='day' rel='2020-04-14'>14</td><td class='day' rel='2020-04-15'>15</td><td class='day' rel='2020-04-16'>16</td><td class='day' rel='2020-04-17'>17</td><td class='day' rel='2020-04-18'>18</td></tr><tr><td class='day' rel='2020-04-19'>19</td><td class='day' rel='2020-04-20'>20</td><td class='day' rel='2020-04-21'>21</td><td class='day' rel='2020-04-22'>22</td><td class='day' rel='2020-04-23'>23</td><td class='day' rel='2020-04-24'>24</td><td class='day' rel='2020-04-25'>25</td></tr><tr><td class='day' rel='2020-04-26'>26</td><td class='day' rel='2020-04-27'>27</td><td class='day' rel='2020-04-28'>28</td><td class='day' rel='2020-04-29'>29</td><td class='day' rel='2020-04-30'>30</td><td colspan='2'>&nbsp;</td></tr></table>

2

Answers


  1. Try getting the element that the calendar is in and setting its .innerHTML value to the output:

    element.innerHTML = response;
    

    If the calendar is in an element with other elements, you could remove the calendar and use the += operator to add it to the end, or make a div for the calendar.

    Login or Signup to reply.
  2. Assuming you want to replace the current .calendar with the new HTML returned by the AJAX call:

    success: (response) => {
        $('.calendar').replaceWith( response );
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search