Jen2020
Jen2020

Reputation: 11

Jquery won't work til 2nd click

I'm calling this code:

function LickEx(pickID){
    alert('yo');
    $(pickID).datepicker({
        format : "mm/dd/yyyy",
        autoclose : true
    });
}

With: onClick="LickExpDt('#example2');"

The 'alert' fires every time, but the '.datepicker' will only fire if I click on it once, click somewhere else, then click on it a 2nd time (along with the 'alert) .. then it acts like normal.
I tried wrapping the function in a $(document).ready(function () { :

$(document).ready(function () {
    function LickEx(pickID){
        alert('yo');
        $(pickID).datepicker({
            format : "mm/dd/yyyy",
            autoclose : true
        });
    }
});

Nothing fires and I get a "Uncaught TypeError: LickEx is not a function"
I need to call LickEx this way so that I can pass the variable in as a parameter (it's the ID of the input box)
is there a way to pass a variable of the ID of input box being clicked on with this:

$(document).ready(function () {
    $([put_var_here]).datepicker({
        format : "mm/dd/yyyy",
        autoclose : true
    });
});

OR fix the original?

Upvotes: 1

Views: 72

Answers (3)

swatkins
swatkins

Reputation: 13630

The first click just binds the datepicker to the element - the second click actually activates it. That's why you don't see the calendar until that second click.

You will need to bind the element before the click.

Upvotes: 0

Sushanth --
Sushanth --

Reputation: 55740

Why not just have a class on the element(s) for which you want to add the date picker functionality ?

 $('.datepicker').datepicker({

The reason it only works on the second click in your case is that you are attaching the datepicker, only when you click on that element. So it takes an additional click to see it.

Upvotes: 4

Bun
Bun

Reputation: 3137

Add a # in front of ID name and . for class name.

$(document).ready(function() {
    $('#pickID').datepicker({
        format : "mm/dd/yyyy",
        autoclose : true
    });
});

Upvotes: -1

Related Questions