Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am trying to set default value of "End Time" field in Calender NewForm.aspx page. Here is my code:

var txtDate = $("#" + $("label:contains('End Time Date')").attr("for"));
var cboHours = $("#" + $("label:contains('End Time Hours')").attr("for"));
var cboMinutes = $("#" + $("label:contains('End Time Minutes')").attr("for"));

txtDate.val("1/4/2013");
cboHours.val("9 AM");
cboMinutes.val("30");

Through break point, I can see that values are properly set on respective dropdowns of "End Time" field, but after my JavaScript code, SharePoint change back those values to +1 hour of current selected "Start Time". If "Start Time" value is "1/4/2013 6:30 AM", SharePoint change back the value of "End Time" to "1/4/2013 7:30 AM".

I am wondering if there is any to set value of "End Time" through Javascript. Or else, if there is any Javascript hack to remove dependency between "Start Time" and "End time"?

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted
+100

In order to init form fields on the client side make sure that your code is executed after page is loaded, for example place your call into SharePoint specific function _spBodyOnLoadFunctionNames

function initEndDateEvent()
{
   var txtDate = $("#" + $("label:contains('End Time Date')").attr("for"));
   var cboHours = $("#" + $("label:contains('End Time Hours')").attr("for"));
   var cboMinutes = $("#" + $("label:contains('End Time Minutes')").attr("for"));

   txtDate.val("1/9/2013");
   cboHours.val("9 AM");
   cboMinutes.val("30");
}


_spBodyOnLoadFunctionNames.push("initEndDateEvent");

New form for Calendar for this case will look like this

enter image description here

share|improve this answer
    
Awesome! It just worked. Appreciate your help. –  Firoz Ansari Jan 8 '13 at 15:32
    
I'm glad it helped :) –  Vadim Gremyachev Jan 8 '13 at 15:35
add comment

You can disable modal windows using

Library/List Settings > Advanced Settings > "Launch Forms in a Dialog" > No

Turns out there is a script that you can load for your newform.aspx page (that being the page you gets when not using modal windows) that will handle the start and end times using jquery.

Source: http://blog.pentalogic.net/2009/09/setting-default-duration-for-new-calender-events/

HTH

share|improve this answer
    
Thanks Mike. I do want to open the form as modal dialog box. Is there anyway I can disable that Javascript code which drive "End Time"? –  Firoz Ansari Jan 7 '13 at 18:21
    
You may have to create your own new modal dialog window. blog.mastykarz.nl/… –  Mike Jan 7 '13 at 19:17
add comment

When are you firing your code? I often use $("#OnetListID").ready() when trying to change fields when the page loads.

share|improve this answer
    
I just tried $("#OnetListID").ready(), it still showing same issue. –  Firoz Ansari Jan 7 '13 at 19:28
    
Sorry, I just listed that out of memory. The wrapper function should be $("#onetIDListForm").ready() Also be sure to put this on the page loaded by the modal dialog –  Blue Murphy Jan 7 '13 at 20:55
    
No luck with $("#onetIDListForm").ready(). I appreciate your help. –  Firoz Ansari Jan 8 '13 at 14:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.