Search code examples
javascriptjqueryhtmlsharepointsharepoint-2007

Autofill input text on Sharepoint 2007 using Jquery


im sure this is a simple question but can't understand why this isn't working. I simply want to set the current time automatically to an input field. I am using Jquery at the moment to accomplish the task. I know jquery works because if I do alert (time); it shows me the time as expected.

Just to add, I set the jquery script in a text then referenced into a Content Editor Webpart under the list.

Below is the HTML of my input generated by Sharepoint. I decided to target the title as its the only consisting attribute in there.

<input type="text" class="ms-long" title="Current time" id="ctl00_m_g_dd7a368d_cc10_4464_a245_c7fc87ae6650_ff2_1_ctl00_ctl00_TextField" maxlength="255" name="ctl00$m$g_dd7a368d_cc10_4464_a245_c7fc87ae6650$ff2_1$ctl00$ctl00$TextField">

Below is the Jquery script I am trying to run. A the moment this script does nothing to my input text box.

  <script src="//code.jquery.com/jquery-1.9.1.js"></script>
    <script type="text/javascript">
    jQuery(document).ready(function($) {
    var time = (dNow.getMonth()+1) + '/' + dNow.getDate() + '/' + dNow.getFullYear() + ' ' + dNow.getHours() + ':' + dNow.getMinutes();
    $("input[Title='Current time']").val(time);
    });
    </script>

Any help would appreciated on this.


Solution

  • I tested your code and it works fine if you define the dNow first: tested on Chrome and Opera

     jQuery(document).ready(function($) {
        var dNow = new Date();
        var time = (dNow.getMonth()+1) + '/' + dNow.getDate() + '/' + dNow.getFullYear() + ' ' + dNow.getHours() + ':' + dNow.getMinutes();
        alert(time);
        $("input[title='Current time']").val(time);
     });
    

    If that's not working for you, you have some other problem in your JavaScript, debug and check for other errors