I’m wondering if the following may be possible using something like jQuery. I’ve looked all over but haven’t found a solution.
I am using MVC 3/razor for my project. I have jQuery 1.5.1.
We want a dropdown (select) box that will show a few options:
– Monthly
– Annually
– Weekly
– Date
If the user selects ‘Date’ I’d like to show a calendar box and then record the selected date as the value in the select box. If they click the select box again they can change the values as per the original select values.
In my project there are several of these boxes on the same page. Perhaps I could tie them to a click action via a class value?
The closest request I’ve found to this point was here: jQuery ui.datepicker on 'select' element
… but I’ve not been able to get that working for my project quite yet.
Any help is appreciated.
Update
The added elements which I need to tie to are inserted to the page dynamically. Perhaps this is where my challenge is coming from.
Update
Here is some code I’m using/messing with at this moment:
First – my code is added dynamically from a template. The field that I’m looking to tie to is below and may exist multiple times across the page:
<td><select id="f${FormCount}_PeriodWorked[]" name="PeriodWorked" class="periodworked">
<option value="month">Month</option>
<option value="annual">Annual</option>
<option value="date" class="datepick">Date</option>
</select>
</td>
On the main page, where the form is dynamically added to, I have the following which attempts to add the datepicker to the option ‘date’.
$(".periodworked").live("change", function () {
if ($(this).val() == "date") {
alert("In... now how to throw date picker....");
}
})
Lastly – tying this to respective fields… is there a way to use $(this) for handling that or do I have to come up with some other way to find the specific select I’ve chosen?
UPDATE
Thanks for the help – I managed to get it working. I understand my circumstance is very specific, but here’s the code if it helps anyone else…
First – via CSS I use colors to hide the form fields (same bg color as table – emulating an MS Excel look). The font color for the input field showing the datepicker is using this same color to prevent any text being seen (though if seen it’s only a flicker before the field is hidden).
In my template I modified the cell as below:
<td><select id="f${FormCount}_PeriodWorked[]" name="PeriodWorked" class="periodworked add-date">
<option value="month">Month</option>
<option value="annual">Annual</option>
<option value="date" class="datepick">Date</option>
</select>
<input name="tempDatePicker" type="text" size="1" class="datepicker for-option" style="display:none; color: #EFEFEF;" /></td>
In my main page I had to do a few ‘funny’ things. Here’s the code:
$("select.add-date").live("change", function () {
if ($(this).val() == "date") {
//alert("In... now how to throw date picker....");
$(this).parents('td').find('input.datepicker').show();
$(this).parents('td').find('input.datepicker').datepicker({
onSelect: function (dateText, inst) {
var opt = $('<option />').attr('value', dateText).attr('selected', 'selected').text(dateText);
var select = $(this).parents('td').find('select.add-date');
// NOTE: ommitted code to check if the date is already in the select
$(select).append(opt);
}
}).focus();
} else {
$(this).parents('td').find('input.datepicker').hide();
}
});
$('input.datepicker.for-option').live('blur', function (dateText, inst) {
$(this).hide();
});
You will notice I’ve added the datepicker to the input within the live/change function. For some reason this is the only way it would work (opposed to placing the datepicker outside of the live). This also allowed me to use the onSelect method which pulled the chosen value from the calendar properly where as the blur wasn’t retrieving the value and returned blank results, for whatever reason.
My blur method simply hides the input field containing the calendar.
This all works fine – but in my case I am adding many fields dynamically by copying the first row of a table and appending it to the existing table. Everything worked fine unless you use the datepicker within that row. At that point a class and id is assigned to the input object of the datepicker, which is then copied, and screws everything up. To fix this I added a method to my insertRow function to basically drop the insert and re-write it. God I hope that makes sense… code:
function addLines(formNum, lines) {
for (var l = 0; l < lines; l++) {
var table = document.getElementById("formTable_" + formNum);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for (var i = 0; i < colCount; i++) {
var newcell = row.insertCell(i);
if (table.rows[1].cells[i].innerHTML.search('name="PeriodWorked"') != -1) {
newcell.innerHTML = table.rows[1].cells[i].innerHTML.substr(0, table.rows[1].cells[i].innerHTML.search("<input"));
newcell.innerHTML = newcell.innerHTML +
'<input name="tempDatePicker" type="text" size="1" class="datepicker for-option" style="display:none; color: #EFEFEF;" />';
} else {
newcell.innerHTML = (table.rows[1].cells[i].innerHTML);//.replace(" hasDatepicker", "");
}
//newcell.childNodes[0].id = "f" + formNum + "_" + newcell.childNodes + (1 + parseInt(newcell.childNodes[0].id.substr(newcell.childNodes[0].id.lastIndexOf("_") + 1));
switch (newcell.childNodes[0].type) {
case "text":
newcell.childNodes[0].value = "";
break;
case "checkbox":
newcell.childNodes[0].checked = false;
break;
case "select-one":
newcell.childNodes[0].selectedIndex = 0;
//resetDatePicker();
break;
// default:
// newcell.innerHTML = "";
// break;
}
}
}
}
Again – I appreciate the help!
To answer the jquery part of your question, you could do this:
…and then…
This should work with text box blur, but you might be able to use the datepicker’s onSelect event to add the option to the select. I have used blur because I know it works with live.