I wrote my own Jquery month picker control. The month picker sits inside a <div />
, which should in turn be attach to the <input />
field I call it upon.
To clarify, here is code:
(function ($) {
//dict has to be a serialized dictionary. The dictionary needs to have years (as numbers) as keys and for each year a list of numbers representing the months.
//Only the provided years/month will be serialized as html.
//Sample usage (Razor):
//var json = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model.Periods.ToArray()));
//$('#monthPicker').CondatoMonthPicker(json);
$.fn.CustomMonthPicker = function (dict) {
debugger;
//Construct HTML
var wrapper = $(this);
$monthPickerHeaderHtml = $('some html...');
wrapper.append($monthPickerHeaderHtml);
};
})(jQuery);
As you notice from my code, I am using MVC Razor. Thus, I use this to instantiate the field holding the datepicker:
@Html.TextBoxFor(model => model.TimePeriod, new { @id = "custom-datepicker", @type = "date", @class = "ms-TextField-field" })
However, this generates the following html:
<input autocomplete="off" class="ms-TextField-field" data-val="true" data-val-date="The field TimePeriod must be a date." data-val-required="The TimePeriod field is required." id="custom-datepicker" name="TimePeriod" type="date">
Notice the missing end tag. Going back to my custom date picker function, when I use .append()
on my wrapper (<div />
) object, it spits out the following ouput html:
<input autocomplete="off" class="ms-TextField-field" data-val="true" data-val-date="The field TimePeriod must be a date." data-val-required="The TimePeriod field is required." id="custom-datepicker" name="TimePeriod" type="date">
My custom html goes here...
</input>
As you can see, instead of appending my custom html, it actually inserts it and adds an ending </input>
tag after it, thus preventing my whole construct from showing on my page. Does anybody know why this is happening and how to work around
EDIT: I think I wasn't clear enough with my phrasing. I don't want to set the value of my <input>
field. Somehow, JQuery gets the input element, INSERTS my html and adds a ending tag at then end when it should really add an end tag first and THEN append my custom html after that ending tag so it looks like this:
<input ...></input>
My custom html...