Hopefully someone can help. I am trying to get a SharePoint new item form to raise an error message if an attachment is not included when a certain choice is selected in a drop down menu.

I have the following code but keep getting the error "Unable to get property 'value' of undefined or null reference".

My code is as follows:

<script type="text/javascript" language="javascript">

function PreSaveAction() {

    var DisciplineValue = document.getElementById('WPQ2aa60ac64-a6ae-4d0a-ae7c-60f56ce236e2Discipline’).value;

    var atch = document.getElementById("idAttachmentsTable");

    if(DisciplineValue == "Welding/Grinding"){

        if (atch == null || atch.rows.length == 0)


            alert("Documents attachment is required");

            return false ;

        }else {

            return true;



        return true;



  • I Guess you have copied element id/code from somewhere and quotes(') are miss-matched. Also, is it the correct ID of your choice field? instead of id you can use title property to get the elements easily from SharePoint form(Can you please add screenshot of choice field after inspecting the element? I can help you with correct selector to get the choice element). – Ganesh Sanap Jul 29 '19 at 10:54

I guess your selector inside first document.getElementById is wrong.

Instead of id you can use title property to get the elements easily from SharePoint form.


Consider your choice field's display name is 'My Choice Field' then try using selector like below:

document.querySelector("select[title^='My Choice Field']").value
| improve this answer | |
  • Have you tried this? is it working for you? – Ganesh Sanap Aug 5 '19 at 8:04

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.