user2743057 user2743057 - 3 months ago 22
jQuery Question

Jquery datepicker execute function on load

I have the following script that works fine:

$(function() {
$( "#datepicker" ).datepicker
(
{
altField: '#sheetid',
onSelect: function load() {
$(document).ready(function() {
$.ajax({
type: 'POST',
url: 'test2.php',
data: {sheetid: $('#sheetid').val()},
success: function(data)
{
$("#contentz").html(data);
}
});

});
},
firstDay: 1});
}
);


The script loads data in a div based on the selected date from an inline jq datepicker. But I can't seem to succeed on making it work for the default date, when the page first loads.
I've tried:

$("#datepicker").load(function() {
$(document).ready(function() {
$.ajax({
type: 'POST',
url: 'test2.php',
data: {sheetid: $('#sheetid').val()},
success: function(data)
{
$("#contentz").html(data);
}
});

}
})


but that doesn't seem to work either. Any suggestions?

Answer

I would do it like this:

$(document).ready(function() {
    $("#datepicker").load(function() {
        $.ajax({
            type: 'POST',
            url: 'test2.php',
            data: {sheetid: $('#sheetid').val()},
            success: function(data)
            {
                $("#contentz").html(data);
            }
        });
    });
});

it makes no sense to bind something to the document.ready event on datepicker.load. You can do what you want directly there.

Comments