Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jqueryui datepicker with hidden input

I got a button and a hidden input.

I want the datepicker opens below the button I click it. And the selected date is inserted in the hidden input.

I don't want to create a new button (using datepicker options), and I don't wanna show the input.

<button type="button" class="btn" id="date_button">Select Date...</button>

<input type="hidden" name="date" id="date_field" />

<script>
    $('#date_button').datepicker({
        showOn: "button",
        onSelect: function( dateText ) {
            $('#date_field').val( dateText );
            $('#date_button').text( dateText );
        }
    })
</script>

Any ideas?

like image 567
VMOrtega Avatar asked Jun 25 '12 16:06

VMOrtega


2 Answers

I did this by doing a .show().focus().hide() - works perfectly though it's a bit unclean:

$(document).ready(function() {
    $('input').datepicker();
        $('#mybutton').click(function() {
        $('input').show().focus().hide();
    });
});

http://jsfiddle.net/JKLBn/

like image 55
Rother Fuchs Avatar answered Nov 15 '22 01:11

Rother Fuchs


This is a little hacky, but it seems to work okay:

  1. Attach the datepicker to the input instead of the button.
  2. Reposition the datepicker to be underneath the button when it opens.
var $button = $("#date_button"),
    left = $button.offset().left,
    top = $button.offset().top + $button.height();

$('#date_field').datepicker({
    onSelect: function(dateText) {
        $('#date_field').val(dateText);
        $button.text(dateText);
    },
    beforeShow: function (event, ui) {
        setTimeout(function () {
            ui.dpDiv.css({ left: left, top: top });      
        }, 5);               
    }
});

$button.on("click", function() {
    $("#date_field").datepicker("show");
});

Example: http://jsfiddle.net/StUsH/

like image 1
Andrew Whitaker Avatar answered Nov 15 '22 03:11

Andrew Whitaker