drip
drip

Reputation: 12943

Datepicker click event not firing

A quick question that I can't wrap my head around.

We have a simple Datepicker (jQuery UI) with a click event on the td and nothing else. But no matter what I try the click event will not fire.

Example: https://jsfiddle.net/s63y1w6h/

The click event is delaged, since the html changes each time a td is clicked, but the same "nothing" happens again.

As to why I need to click the a, I need the td data attributes, text and its offeset, which will be harder to get from the onSelect method.

Any idea how to fire up the click event?

Upvotes: 0

Views: 6851

Answers (2)

Kaushal Niraula
Kaushal Niraula

Reputation: 576

You can still use onSelect to fire your logic when a day is clicked

$('.datepicker').datepicker({
  onSelect:function(dt,obj){
    var day = parseInt(dt.substring(3,5));
    var td;
    $("td>a").each(function(index,elm){
     if(parseInt($(elm).html()) === day){
       td = $(elm).parent();
       return;
      }
    })
    console.log(td)
    alert("You're welcome!!!")
  }
});

Wrote some hack logic to get the td based on the day selected

Working plnkr

Upvotes: 1

Johan Pranata
Johan Pranata

Reputation: 317

Try to put your code inside

$(document).ready(function(){
    //Your code here
});

JSFiddle: https://jsfiddle.net/s63y1w6h/1/

EDIT: Try to put your code inside

$(document).ready(function(){
    $('.datepicker').datepicker({
        onSelect: function(){
            //your code here
        }  
    });
});

https://jsfiddle.net/s63y1w6h/2

Upvotes: 0

Related Questions