title: Return clicked date value from calendar
body: <p>I want to return chosen by click date in alert window. For example, if today is 12 April and I clicked 1 January of 2016 alert should show <code>2016-1-1</code>.</p>
  
  <p>Here is my Javascript code:</p>
  
  <pre class="lang-js prettyprint-override"><code>var Cal = function(divId) {
    //Store div id
    this.divId = divId;
    // Days of week, starting on Sunday
    this.DaysOfWeek = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
    // Months, stating on January
    this.Months = ['January', 'February', 'Marhc', 'April', 'May', 'June', 'July', 'August', 'September', 'Octomber', 'November', 'December'];
    // Set the current month, year
    var d = new Date();
    this.currMonth = d.getMonth();
    this.currYear = d.getFullYear();
    this.currDay = d.getDate();
  };
  // Goes to next month
  Cal.prototype.nextMonth = function() {
    if (this.currMonth == 11) {
      this.currMonth = 0;
      this.currYear = this.currYear + 1;
    } else {
      this.currMonth = this.currMonth + 1;
    }
    this.showcurr();
  };
  // Goes to previous month
  Cal.prototype.previousMonth = function() {
    if (this.currMonth == 0) {
      this.currMonth = 11;
      this.currYear = this.currYear - 1;
    } else {
      this.currMonth = this.currMonth - 1;
    }
    this.showcurr();
  };
  // Show current month
  Cal.prototype.showcurr = function() {
    this.showMonth(this.currYear, this.currMonth);
  };
  // Show month (year, month)
  Cal.prototype.showMonth = function(y, m) {
    var d = new Date()
      // First day of the week in the selected month
      ,
      firstDayOfMonth = new Date(y, m, 1).getDay()
      // Last day of the selected month
      ,
      lastDateOfMonth = new Date(y, m + 1, 0).getDate()
      // Last day of the previous month
      ,
      lastDayOfLastMonth = m == 0 ? new Date(y - 1, 11, 0).getDate() : new Date(y, m, 0).getDate();
    var html = '&lt;table&gt;';
    // Write selected month and year
    html += '&lt;thead&gt;&lt;tr&gt;';
    html += '&lt;td colspan="7"&gt;' + this.Months[m] + ' ' + y + '&lt;/td&gt;';
    html += '&lt;/tr&gt;&lt;/thead&gt;';
    // Write the header of the days of the week
    html += '&lt;tr class="days"&gt;';
    for (var i = 0; i &lt; this.DaysOfWeek.length; i++) {
      html += '&lt;td&gt;' + this.DaysOfWeek[i] + '&lt;/td&gt;';
    }
    html += '&lt;/tr&gt;';
    // Write the days
    var i = 1;
    do {
      var dow = new Date(y, m, i).getDay();
      // If Sunday, start new row
      if (dow == 0) {
        html += '&lt;tr&gt;';
      }
      // If not Sunday but first day of the month
      // it will write the last days from the previous month
      else if (i == 1) {
        html += '&lt;tr&gt;';
        var k = lastDayOfLastMonth - firstDayOfMonth + 1;
        for (var j = 0; j &lt; firstDayOfMonth; j++) {
          html += '&lt;td class="not-current"&gt;' + k + '&lt;/td&gt;';
          k++;
        }
      }
      // Write the current day in the loop
      var chk = new Date();
      var chkY = chk.getFullYear();
      var chkM = chk.getMonth();
      if (chkY == this.currYear &amp;&amp; chkM == this.currMonth &amp;&amp; i == this.currDay) {
        html += '&lt;td class="today"&gt;' + i + '&lt;/td&gt;';
      } else {
        html += '&lt;td class="normal"&gt;&lt;a href="" id="dge" onClick="MyFunction();"&gt;' + i + '&lt;/a&gt;&lt;/td&gt;';
      }
      // If Saturday, closes the row
      if (dow == 6) {
        html += '&lt;/tr&gt;';
      }
      // If not Saturday, but last day of the selected month
      // it will write the next few days from the next month
      else if (i == lastDateOfMonth) {
        var k = 1;
        for (dow; dow &lt; 6; dow++) {
          html += '&lt;td class="not-current"&gt;' + k + '&lt;/td&gt;';
          k++;
        }
      }
      i++;
    } while (i &lt;= lastDateOfMonth);
    // Closes table
    html += '&lt;/table&gt;';
    // Write HTML to the div
    document.getElementById(this.divId).innerHTML = html;
  };
  // On Load of the window
  window.onload = function() {
      // Start calendar
      var c = new Cal("divCal");
      c.showcurr();
      // Bind next and previous button clicks
      getId('btnNext').onclick = function() {
        c.nextMonth();
      };
      getId('btnPrev').onclick = function() {
        c.previousMonth();
      };
    }
    // Get element by id
  function getId(id) {
    return document.getElementById(id);
  }
  
  function MyFunction(){
    alert(document.getElementById('dge').text);
  }
  </code></pre>