title: How to launch function every time part of page reloads
body: <p>i am making app in Django and i would like to launch function every time that i reload calendar. When user clicks arrows on the side of calendar it reloads division with calendar and shows new month. When user clicks on one of the days new content should show up (this is done by assigning content to each day using setAttribute(&quot;onclick&quot;, ...) function). Content showing works until i change month. From what i understand it is because i assign function to only existing DOM elements and not the new ones. I tried assigning it to new elements in various ways but it never worked. Is there some way to do it?</p>
  <p>Month changing is done by ajax load function.</p>
  <pre><code>for (var x=0; x &lt; days.length; x++) {
      days[x].setAttribute(&quot;onclick&quot;, `setDay(
      &quot;${String(days[x].innerHTML)}&quot;,
      &quot;${(month[1].innerHTML)}&quot;
      )
  `)}
  
  $('#right').click(function () {
      let mth = $('.month')[1].innerHTML.split(&quot; &quot;)
      let month = parseInt(monthsObj[mth[0]])+1
      let year = parseInt(mth[1])
      if (month == 13) {
          month = 1
          year += 1
          $('#newCal').html('').load(
              &quot;{% url 'calendarChange' monthNumber=4 yearNumber=2022 %}&quot;.replace(&quot;4&quot;, month).replace(&quot;2022&quot;, year)
          );
          } else {
          $('#newCal').html('').load(
              &quot;{% url 'calendarChange' monthNumber=4 yearNumber=2022 %}&quot;.replace(&quot;4&quot;, month).replace(&quot;2022&quot;, year)
          );
      }
  })
  </code></pre>
  <p>I tried putting that for cycle in function and putting it into ajax click. Ajax click then worked but for cycle didn't.</p>