RollerCosta
RollerCosta

Reputation: 5196

Jquery DatePicker to multiple elements.?

How to apply Jquery DatePicker to multiple elements. at once??
Suppose i have 3 TextBoxes

 <td class="textBox">@Html.LabelFor(model => model.textbox1)
 <td class="textBox">@Html.LabelFor(model => model.textbox2)
 <td class="textBox">@Html.LabelFor(model => model.textbox3)


Now here i want to apply datepicker to all three textBoxex at once.

I also want to know is it possible to set mm-yy as datepicker format???

Updated
How to set regional datepicker dateFormat and dateRange at one step??

Upvotes: 1

Views: 3544

Answers (5)

RollerCosta
RollerCosta

Reputation: 5196


Changed @Html.LabelFor to

@Html.TextBoxFor(model=>model.textbox1,new{@class=textBox1}) @Html.TextBoxFor(model=>model.textbox1,new{@class=textBox1}) @Html.TextBoxFor(model=>model.textbox1,new{@class=textBox1})

Then used below script

<script type="text/javascript">

$(function () {
    alert(abc);
    $(".textBox").datepicker({
        changeMonth: true,
        changeYear: true,
        dateFormat: 'mm-yy',
        yearRange: '1900:2012'
    });
    $.datepicker.setDefaults($.datepicker.regional[""]);
    $(".textBox").datepicker($.datepicker.regional["en"]);
    $(".textBox").datepicker("option", $.datepicker.regional[anyculture]);
});

Upvotes: 0

Yorgo
Yorgo

Reputation: 2678

you should use

@Html.TextBoxFor(model=>model.text1, new {@class="date-picker"});
<script>
$(".date-picker").datepicker();
</script>

OR

1- Add new Editor inside Views->Shared->EditorTemplates DateTime

@model System.DateTime?
@if (Model.HasValue)
{
    @Html.TextBox("", Model.Value.Date.ToShortDateString(), new { data_datepicker = "true", style="width:80px;" })      
}
else
{
    @Html.TextBox("", null, new { data_datepicker = "true", style="width:80px;" })      
}
<script type='text/javascript'>
    $(function () {
        $(":input[data-datepicker]").datepicker({
            showOtherMonths: true,
            selectOtherMonths: true,
            showOn: "both",
            showAnim: "slide",
            showButtonPanel: true,
            changeMonth: true,
            changeYear: true,
            numberOfMonths: 2,
            buttonImage: "@Url.Content("~/xtras/images/calendar.png")",
            buttonImageOnly: true
        });
    };
</script>

2- Use EditorFor helper for view

@Html.EditorFor(model=>model.date1)

Upvotes: 0

akiller
akiller

Reputation: 2472

If you give each of your date inputs a class (e.g. cdate) you can use something like this:

$(function() {
   $(".cdate").datepicker({
      dateFormat: 'mm-y'
   });
});

http://jsfiddle.net/z84td/2/

Upvotes: 1

Stefan
Stefan

Reputation: 5662

$(".textBox").datepicker({ dateFormat: "mm-y" });

Source: http://jqueryui.com/demos/datepicker/#date-formats

Upvotes: 3

Jamie Dixon
Jamie Dixon

Reputation: 53991

You should just be able to attach the datepicker to the class:

$(".textBox").datepicker();

Upvotes: 0

Related Questions