none
PreSaveAction is not firing

    Question

  • I have a custom edit form for my list to which I have added the following code under "PlaceHolderMain". PreSaveAction does not seem to fire as I don't see the alerts upon clicking Save. Thanks for the help.

    <asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">
    <script type="text/javascript">
     var $j = jQuery.noConflict();
     $j(document).ready(function() {
      function PreSaveAction() {
       alert("here");
       var txtStatus = $j(":input[title='Status']").val();
       if(txtStatus == "In Progress"){       
       alert("Invalid Status");       
       var statusfocus = $j(":input[title='Status']");       
       statusfocus .focus();       
       return false;}
       return true;
      }

    });

    </script>



    • Edited by vmrao Tuesday, October 11, 2011 6:24 PM
    Tuesday, October 11, 2011 6:23 PM

Answers

All replies

  • Hi vmrao,

    You have only declared the function. You need to call the function also. I would do it the following way:

     

    <script type="text/javascript">
     var $j = jQuery.noConflict(); 
     $j(document).ready(function() {
         
           PreSaveAction();
    
    });
    
     function PreSaveAction() { 
       alert("here");
       var txtStatus = $j(":input[title='Status']").val(); 
       if(txtStatus == "In Progress"){        
       alert("Invalid Status");        
       var statusfocus = $j(":input[title='Status']");        
       statusfocus .focus();        
       return false;}
       return true;
      }
    
    </script>
    


     


    Tuesday, October 11, 2011 7:26 PM
  • I though PreSaveAction is a SP default function and need not be called as it is called by PreSaveItem automatically when clicked on Save button. Can someone please clarify this.
    Tuesday, October 11, 2011 8:46 PM
  • Did the above code work?

    also, try it without the  $j(document).ready()

     

    <script type="text/javascript">
     var $j = jQuery.noConflict(); 
     function PreSaveAction() { 
       alert("here");
       var txtStatus = $j(":input[title='Status']").val(); 
       if(txtStatus == "In Progress"){        
       alert("Invalid Status");        
       var statusfocus = $j(":input[title='Status']");        
       statusfocus .focus();        
       return false;}
       return true;
      }
    
    </script>

    Also, checkout this link on the PreSaveAction() function:

    http://ghamson.wordpress.com/2011/01/25/useful-javascript-function-presaveaction-sp2010-sharepoint-ps2010-projectserver-in/

    • Edited by Vardhaman Deshpande Tuesday, October 11, 2011 8:57 PM
    • Marked as answer by vmrao Wednesday, October 12, 2011 8:17 PM
    Tuesday, October 11, 2011 8:56 PM
  • Deleting document.ready did the trick. Thanks.

    Wednesday, October 12, 2011 8:16 PM
  • Dear Vardhaman (and all) --

    I tried what is suggested in this thread thus far, but  SharePoint 2013 when I am creating a new item with the NewDocSet.aspx page, which is based on a custom Content Type. Why? 

    Here are the details...

    --- In my SiteAssets folder, I have these files...

    jquery.js (which is jQuery JavaScript Library v1.11.1)

    CustomScripts.js (which is my custom JS file)

    --- On my Master Page, I have this...

    ...blah...

    <SharePoint:CustomJSUrl runat="server" />

    <!-- NOTE. 2014-11-17. MFK. New links. Begin -->

    <SharePoint:ScriptLink language="javascript" name="~site/siteassets/jquery.js" runat="server" OnDemand="False" LoadAfterUI="True" />

    <SharePoint:ScriptLink language="javascript" name="~site/siteassets/CustomScripts.js" runat="server" OnDemand="False" LoadAfterUI="True" />

    <!-- NOTE. 2014-11-17. MFK. New links. End -->

    <SharePoint:SoapDiscoveryLink runat="server" />

    ...blah...

    --- In my CustomScripts.js file I have this...

    alert('Found me.'); //Does show.

    function PreSaveAction()
    {
     alert("Found PreSaveAction."); //Does NOT show.

     return false;
    }

    --- Do you have a solution?

    Please advise.

    Thanks.


    -- Mark Kamoski

    Tuesday, November 18, 2014 3:35 PM
  • Hi Mark and Everyone,

    So what I have observed, developing in SharePoint 2013, regarding PreSaveAction is that you need to have this function declared outside all on-load functions like

    (function () {
        [code blocks on load here]
    })();

    and

    $(document).ready(function () {
       [code blocks on form ready here]
    });

    The correct way of doing it is like this:

    function PreSaveAction() {
       [Codes for pre save action]
    }
    
    (function () {
       $(document).ready(function () {
          [codes]
       });
    
       [some more codes]
    })();

    Friday, January 30, 2015 9:01 AM