Question

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?

Was it helpful?

Solution

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>

OTHER TIPS

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?)

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top