0

I am working on a team site inside sharepoint 2013. and inside my custom list i added this javascript to prevent closing an item unless the user enter a multi-line text field named "Analysis":-

<script language="javascript" type="text/javascript"> 
function PreSaveItem(){
    var result = true;
    var status=$("select[id*='UserStatus_'] option:selected").text();
    if( status == "Closed")
    {
    var analysis = $('input[id^="Analysis_"]').val().trim();
    alert(analysis);
    if( analysis == "<p>​</p>"){
       alert("Please Enter Analysis before closing the item");
       result = false;
   }
    }
    return result;
}
</script>

But I am not sure why the script is not getting the updated value for the input[id^="Analysis_"] as we update the list item fields?. For example let say i set the status = "Closed" + left the "Analysis" input empty, and i click on "Save" >> so i will get an error message correctly. but if i enter values inside the "Analysis" input and i click on "Save" again, i will still get the same error and i popup alert(analysis); will not take the updated text which i entered. is this a caching problem inside the PreSaveItem() function?

  • 1
    What kind of field is 'Analysis'? Single line, multi-line, etc.? If multi-line, is it plain, rich text, or enhanced rich text? – Joe McShea Feb 15 at 20:16
  • @JoeMcShea it is multi-line enhanced.. – SharePoint TestDev Feb 15 at 22:02
0

The following code for your reference.

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
function PreSaveItem(){ 
    var status=$("select[id*='UserStatus_'] option:selected").text();
    if(status == "Closed"){
        var analysis = jQuery("div[id^='Analysis_'][id$='TextField_inplacerte']");       
        if(analysis.text().trim() == ""){
            alert("Please Enter Analysis before closing the item");
            return false;
        }
    }
    return true;
}
</script>
2

You are not getting updated text because input tag stores old saved text (it is a hidden input by the way). What you need to retrieve is a multiline enabled property div content; I suggest you to inspect your form source.

$('div[id^="Analysis_"].ms-rtestate-write')
// innerText and innerHTML element properties return content

Apart from that, you should not use (override!) PreSaveItem as it is defined by SharePoint; use PreSaveAction instead (What is the difference between PreSaveAction and PreSaveItem?)

  • yes you are correct the text is stored inside a Div .. but i did not get your point ,, why i should not use PreSaveItem? can u explain it in more details? – SharePoint TestDev Feb 17 at 0:46
  • 1
    The function "PreSaveItem" checks if a function with name "PreSaveAction" exists or not and if it exists invoke it and return true/false or return "true" by default. It's not recommended to override "PreSaveItem". Also, "PreSaveItem" is defined in form.js and automatically included while "PreSaveAction" only exists if it is defined by the user. – Ravi Feb 17 at 5:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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