0

I am using bootstrap datepicker inline, when i click on a year, it is fine as i see ciao in console, however, when I click on prev or next, and then I click on a year, console is not showing ciao. I have tried a lot of different things, one of which is the following and goDateClick() is firing but not the click event within it

  goDateClick();

  $("#sandbox-container .prev").on("click", function(e) {
    goDateClick();
  });

  $("#sandbox-container .next").on("click", function(e) {
    goDateClick();
  });

  function goDateClick() {
    $("#sandbox-container span").on("click", function(e) {
       console.log("ciao");
    });
  };

Try it, click a year and it's fine, click next or prev and the year won't show up

jsFiddle here

I have tried the suggested answer as per "duplicated" and it doesn't work check http://jsfiddle.net/vwmHF/281/

rob.m
  • 8,256
  • 14
  • 65
  • 119

3 Answers3

1

Instead of creating new listeners every time, only add a single listener to the container, and then select e.target to get the element that was clicked on. Replace the whole script with this:

document.querySelector('.datepicker').addEventListener('click', e => {
  if (e.target.tagName !== 'SPAN') return;
  document.querySelector('#my_year').textContent = e.target.textContent;
  e.target.classList.add('selected_year', 'active', 'focused');
});
Vishal Taj PM
  • 1,223
  • 10
  • 20
CertainPerformance
  • 260,466
  • 31
  • 181
  • 209
1

you can find a minimal code which only used datepicker events please view the snippet below :

click to view demo

$('#sandbox-container div').datepicker({
  format: "yyyy",
  viewMode: "years", 
  minViewMode: "years",
}).on('changeYear', function(e) {
  $('#my_date').text(e.date.getFullYear());
});

so here I just used the default on('changeYear', function(e){}) to trigger whenever we click on the year.

Vishal Taj PM
  • 1,223
  • 10
  • 20
  • oh thank you! That's actually what I was trying, I didn't see getFullyear, link to a doc pelase? – rob.m Mar 24 '18 at 09:08
  • it's a javascript date function you can find it here [Javascript Date Functions](https://www.w3schools.com/js/js_date_methods.asp) – Vishal Taj PM Mar 24 '18 at 09:11
  • oh right didn't think of using plain js functions, thought to just follow the plugin doc, thanks a lot – rob.m Mar 24 '18 at 09:12
0

Just use native bootstrap-datepicker's events changedate and changeyear

and here is the changed code in js fiddle

$('#sandbox-container div').datepicker({
  format: "yyyy",
  viewMode: "years", 
  minViewMode: "years"
}).on("changeDate", function(e) {
   goDateClick(e)
}).on("changeYear", function(e) {
   goDateClick(e)
});

function goDateClick(e) {
    $this = $(e.target)
     $("#my_year").text($this.text());
    $("#sandbox-container span").removeClass("selected_year").removeClass("focused").removeClass("active");
    $this.attr("class", "selected_year").addClass("active focused");
};

You can see that I added .on("changeDate") and .on("changeYear") and pass event objects to your cutom function. And in your function goDateClick there is no need to add on-click handler as the event already handled by bootstrap-datepickers's native events. Only proper handling of e and e.target is needed.

Vishal Taj PM
  • 1,223
  • 10
  • 20
seethrough
  • 714
  • 4
  • 15