31

I have a datepicker control setup using the JQuery UI, I am also using the JQuery UI themes which provide a bunch of default icons that I want to use.

The DatePicker allows for specifying a specific image, i.e.:

<script type="text/javascript">
  $(document).ready(function() {
    $("#DateFrom").datepicker({ showOn: 'button', buttonImageOnly: true, buttonImage: 'images/ui-icon-calendar.png' });
  });
</script>  

To display an icon from the icon set you use something like:

<span class="ui-icon ui-icon-calendar"></span>

Is there an easy to integrate the two or do I just need to hack out the styles/images manually?

3
  • 2
    I'd like this, too; I submitted a ticket dev.jqueryui.com/ticket/5081 Commented Jan 20, 2010 at 17:45
  • My ticket was marked Wont-Fix with a comment indicating the the buttn support may be dropped entirely in the future... I guess you're on your own :/ Commented Jan 23, 2010 at 3:11
  • You can also try to connect datepicker by following code: stackoverflow.com/questions/5082204/… Commented Oct 15, 2012 at 10:33

11 Answers 11

30

I got it working by doing this

$("#DateFrom").datepicker({
    showOn: 'button'
}).next('button').button({
    icons: {
        primary: 'ui-icon-calendar'
    }, text:false
});

Just modifies the button that it inserts next to your input for the calendar. By default they throw three ellipses in the text, so I remove that as well.

Sign up to request clarification or add additional context in comments.

5 Comments

This answer actually uses the jQuery icons
And if you want the icon centred in the button use .button({ icons: { primary: 'ui-icon-calendar' }, text: false })
This solution still works. Slightly counter-intuitive: the datepicker option "buttonImageOnly: true" must be removed for this trick to work.
Its funny that this is still working/viable after all these years. My third answer ever given on SO.
This is great. With new version of JQUI it should be just icon: "ui-icon-calendar". Also good idea to set buttonText: "" on the datepicker otherwise you get "..." in the title which then shows as a hint in most browsers.
27

I suggest putting the image into the input

input.date_picker {
  text-align: center;
  background-image: url("images/ui-icon-calendar.png");
  background-position: right center;
  background-repeat: no-repeat;
  padding-right: 18px;
  width: 78px;
}

Like

date_picker_example

I used this icon from the nice tango icons project also available in png

Advantages:

  • Independent from javascript
  • Loads instantly with the DOM
  • Future date inputs easy to add

1 Comment

As @Nipup points out this is how jQuery does it jqueryui.com/demos/datepicker/#icon-trigger
14

I'm afraid you'll have to do that manually. By default, the Datepicker plugin uses the following HTML to insert the specified buttonImage:

<img title="..." alt="..." src="images/ui-icon-calendar.png" class="ui-datepicker-trigger">

By the way, you might want to use...

$(function() {
 // your code here
});

...instead of...

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

7 Comments

Why would use use "$(function() {" instead of "$(document).ready(function() { "?
1) Because it's shorter. 2) Because it's easier to remember. 3) Because it does exactly the same thing, since it's an alias for $(document).ready(function() { ... }.
Assuming you're not working in an old version, "$(function(){})" isn't available in all versions ;)
See jqueryui.com/demos/datepicker/#icon-trigger as @Nipun suggests. You don't need to do it manually.
@RickAndMSFT Note that this answer was correct at the time of writing (back in 2009). Things have changed since then.
|
3

The best way would be to;

First use the javascript as you have already used.

 <script type="text/javascript">
   $(document).ready(function() {
        $("#DateFrom").datepicker({ showOn: 'button', buttonImageOnly: true, buttonImage: 'images/ui-icon-calendar.png' });
   });
 </script>

Then, add the following css code;

<style type="text/css">
.ui-datepicker-trigger
{
        padding:0px;
        padding-left:5px;
        vertical-align:baseline;

        position:relative;
        top:4px;
        height:18px;
}
</style>

This css will help you to align the calendar image correctly as you require.

1 Comment

Again this isn't using the jquery icon set as requested
1

Just a minor follow on to Evs' answer... (great help and thank you Evs !).

It works in IE8 too, and I added CSS to change the cursor on hover:

button.date-picker-button-hidden:hover
{
    cursor: pointer; 
}

Comments

0

http://jqueryui.com/demos/datepicker/#icon-trigger

A very good example with the sample code is provided.

1 Comment

This example uses the datepicker property that lets you specify a specific image. The question asked is how to use the jquery icon set instead.
0

Borrowing from and extending Loktar's answer, you could try something like this, note I have only tested this in FF10, but I can't see why it wouldn't work in other browsers.

$('.date-picker').datepicker({showOn: 'button'})
    .next('button').addClass('date-picker-button-hidden')
    .after($('<span/>')
        .addClass('ui-icon').addClass('ui-icon-calendar').addClass('date-picker-icon'));

Note that in this example, date-picker-button-hidden and date-picker-icon are my own classes, since I don't like styling the jQuery CSS classes directly. Apply the following CSS:

span.date-picker-icon
{
    display: inline-block;
    z-index: -1;
    position: relative;
    left: -16px;
}

button.date-picker-button-hidden
{
    opacity: 0.0;
    filter: alpha(opacity=0);
    height: 16px;
    width: 16px;
    margin: 0;
    padding: 0;
}

This is effectively placing a regular jQuery span icon behind the button. The button is completely see-through / opaque (via the CSS), however because it is still on top of the icon, clicking the icon triggers the button click event.

1 Comment

I tried this because Loktar's answer aligns the icon to the left of the button. But I found that the position:relative on the span made it disappear off-screen altogether. Perhaps it is reliant on positioning of some ancestor element...Anyway I went back to Loktar's solution and added text: false to the button, and now the icon is centred and the button is using the jQuery-UI theme, so I'm not sure what this solution brings to the table.
0

I used this jquery code to get a datepicker for a textinput field and a jquery UI icon to work as a image on the datepicker button.

// Apply jQuery UI DatePicker to all controls with class = datepicker
    $('.datepicker').datepicker({
        showWeek: true,
        dateFormat: "yy-mm-dd",
        buttonImage: "ui-icon-calendar",
        regional: "sv",
        minDate: "-10Y",
        maxDate: "+10Y",
        showButtonPanel: true,
        buttonImageOnly: false,
        showWeekNumber: true,
        firstDay: 1,
        showOtherMonths: true,
        selectOtherMonths: true,
        changeMonth: true,
        changeYear: true,
        showOn: "both"

    }).next('button').text('Show calendar').button({ icons: { primary: 'ui-icon-calendar' }, text: false });

Comments

0

change: to showOn: "both",//button

<input type="text" name="date_from" id="date_from" />        
<input type="text" name="date_to" id="date_to" />

$(function() {
$("#date_from").datepicker({
    changeMonth: true,
    changeYear: true,
    numberOfMonths: 1,
    showOn: "both",//button
    buttonImageOnly: false,
    showAnim: "slideDown",
    dateFormat: "yy-mm-dd",
    onClose: function(selectedDate) {
        $("#date_to").datepicker("option", "minDate", selectedDate);
    }
});
$("#date_to").datepicker({
    changeMonth: true,
    changeYear: true,
    numberOfMonths: 1,
    showOn: "both",//button
    buttonImageOnly: false,
    showAnim: "slideDown",
    dateFormat: "yy-mm-dd",
    onClose: function(selectedDate) {
        $("#date_from").datepicker("option", "maxDate", selectedDate);
    }
});
});

http://jsfiddle.net/wimarbueno/fpT6q/

Comments

0

In going the route of manually setting up a button/icon, you could use this example from How to combine the jQueryUI DatePicker's Icon Trigger with Bootstrap 3's Input Groups to have a JS click event set the focus on the relevant date input field, thereby triggering the jQueryUI date functionality to appear.

Or, as the documentation states, you can use the show method to display the date picker (note that "If the datepicker is attached to an input, the input must be visible for the datepicker to be shown.").

$( ".selector" ).datepicker( "show" );

This allows you to use whatever button/image/icon next to an input field, and be able to click on the button/image/icon to trigger the date functionality for the input field.

Comments

0

I got this to work by adding the HTML for my icon button directly into the buttonText option and disabling the buttonImage option.

$('.datepicker').datepicker({
    showOn: "both",
    buttonText: '<i class="icon-calendar"></i>',
    dateFormat : "DD, MM d"
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.