- I have a php function, which generates calendar for the current
month with button for next and previous months. - I also have a js file, which monitors the click event and generates calendar for next or
previous month. - 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 = '‹ Previous'/>
<div class = 'month'>April 2020</div>
<a class='next_button' href='TBD'>Next ›</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'> </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'> </td></tr></table>
2
Answers
Try getting the element that the calendar is in and setting its .innerHTML value to the output:
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.
Assuming you want to replace the current
.calendar
with the new HTML returned by the AJAX call: