Reputation: 5196
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
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
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
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'
});
});
Upvotes: 1
Reputation: 5662
$(".textBox").datepicker({ dateFormat: "mm-y" });
Source: http://jqueryui.com/demos/datepicker/#date-formats
Upvotes: 3
Reputation: 53991
You should just be able to attach the datepicker to the class:
$(".textBox").datepicker();
Upvotes: 0