none
Set time value in document.ready function do not work RRS feed

  • Question

  • Hi everybody

    I've a strange question....

    I have a NewItem Form in witch I have two DateTime Fields that represent the start and the end of the actvity.

    I want to set the  start activity time  at "09:" and the end activity time at "17:"

    I use jQuery on jQuery(document).ready(function() and this code work:

     jQuery("#ctl00_ctl40_g_83c21e26_7fa5_4b83_9920_2b36fb409985_ff31_ctl00_ctl00_DateTimeField_DateTimeFieldDateHours option:contains('09:')").attr('selected', 'selected');
        jQuery("#ctl00_ctl40_g_83c21e26_7fa5_4b83_9920_2b36fb409985_ff41_ctl00_ctl00_DateTimeField_DateTimeFieldDateHours option:contains('17:')").attr('selected', 'selected');

    The strange thing is that I can see the EndTime Value on "17:" but only for few seconds.... Then the value is setted to 1 hour after the StartTime...

    It seems that there is another script that fire AFTER document.ready and re-set the value...

    Then if I try to modify programmatically the EndTime value, it not works...

    Where is my error?

    Thanks to all

    Thursday, March 23, 2017 12:26 PM

All replies

  • Hi,

    Please try the code like this(add a timeout function):

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
    
    _spBodyOnLoadFunctionNames.push("ready");
    
    function ready()
    {
    	console.log("ready");
    
    	setTimeout(function(){
    		console.log('change selected');
    
    		$('#ctl00_ctl41_g_0f24546a_0ac3_42e7_8034_71f6fb237fb0_ctl00_ctl05_ctl03_ctl00_ctl00_ctl04_ctl00_ctl00_DateTimeField_DateTimeFieldDateHours option[selected="selected"]').removeAttr('selected')
    
    		$('#ctl00_ctl41_g_0f24546a_0ac3_42e7_8034_71f6fb237fb0_ctl00_ctl05_ctl03_ctl00_ctl00_ctl04_ctl00_ctl00_DateTimeField_DateTimeFieldDateHours option:contains("5 AM")').attr('selected', 'selected');
    	}, 1000);
    }
    
    
    </script>
    

    Best regards,

    Patrick

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Sunday, March 26, 2017 7:09 AM
    Moderator
  • Hi,

    Any update?

    Best regards,

    Patrick


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Friday, March 31, 2017 11:31 AM
    Moderator