0

I want to set the values of 3 SharePoint list columns when it goes into edit mode using JavaScript in SharePoint Online.

I have a list in a popup form that looks like the below. When the user clicks the Edit button to go into edit mode, I want the EntryDateTime to be set to the current date/time, initials to be set to the initials of the current user, and Project to be set to the current project. How/where can I fire the JavaScript? Popup for Edit Button Fields to populate with default values

1 Answer 1

0

Here is the solution:

<script  src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>
  $(document).on("click", "#Hero-WPQ2 a.ms- heroCommandLink:contains('edit')", function(evt) { 
    var t = setInterval(function() {
    if($("#spgridcontainer_WPQ2_leftpane_mainTable").length) {
        clearInterval(t);
        var d = formatDate(new Date());
        $("#spgridcontainer_WPQ2_leftpane_mainTable tr:last td:eq(1)").text(d);

        var name = ""; // can't be done with this method as the name don't appear on the page, only something like : Jim's Mega project
        $("#spgridcontainer_WPQ2_leftpane_mainTable tr:last td:eq(2)").text(name);

        var currentproject = "";
        var arr = $("h1:contains('Project Communications: Project ')").text().match(/Project\sCommunications:\sProject\s(\d*)\s/);
        if(arr && arr[1])
            currentproject = arr[1];
        $("#spgridcontainer_WPQ2_leftpane_mainTable tr:last td:eq(6)").text(currentproject)
    }
},100);
});

function formatDate(dateVal) {
  var newDate = new Date(dateVal);
  var sMonth = padValue(newDate.getMonth() + 1);
  var sDay = padValue(newDate.getDate());
  var sYear = newDate.getFullYear();
  var sHour = newDate.getHours();
  var sMinute = padValue(newDate.getMinutes());
  var sAMPM = "AM";
  var iHourCheck = parseInt(sHour);

  if (iHourCheck > 12) {
    sAMPM = "PM";
    sHour = iHourCheck - 12;
  }
  else if (iHourCheck === 0) {
    sHour = "12";
  }

  sHour = padValue(sHour);

  return sMonth + "/" + sDay + "/" + sYear + " " + sHour + ":" + sMinute + " " + sAMPM;
}

function padValue(value) {
  return (value < 10) ? "0" + value : value;
}
</script>

Credit to leakim971

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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