1

Thank you for taking the time to read my question.

I have a simple script in a SharePoint NewForm.aspx which initially hides a field on the form and then displays it if Yes is chosen in response to a question field. Here's the code:

<script src="https://code.jquery.com/jquery-1.7.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
 $(document).ready(function() {
    //Hide fields initially 
    $('nobr:contains("abc")').closest('tr').hide();
    //Show field if Yes is chosen from the drop down list
    $("select[title$='My question here?']").change(function()   {
    if($(this).val() == "Yes" ) {
        $('nobr:contains("abc")').closest('tr').show();
    } else {
        $('nobr:contains("abc")').closest('tr').hide();
    }
});
});
</script>

The problem with it is that some of the fields in the form are required fields, and if the user submits the form without completing the required fields, then:

  1. The field that needs to be completed is highlighted, which I want
  2. The script above is rerun and the form field is hidden again, which I don't want

How do I prevent the hide script being rerun?

Hoping someone can help.

3

I'm not a 100% sure on this, but here is what I normally do... Rather than keeping the script from reruning, the script should be able to run again without breaking the behaviour. You are telling the form to perform an action when a field changes, which is good for user input, but the script needs to know what to do if the value is already set (which should be the case scenario for the edit form too).

In this case, I would prompt the script to validate if the value of the field is "Yes" before the user's input.

<script type="text/javascript">

$(document).ready(function() {
    //Validate field initially
    if($("select[title$='My question here?']").val() == "Yes" ) {
        $('nobr:contains("abc")').closest('tr').show();
    } else {
        $('nobr:contains("abc")').closest('tr').hide();
    }

    //Show field if Yes is chosen from the drop down list
    $("select[title$='My question here?']").change(function()   {
    if($(this).val() == "Yes" ) {
        $('nobr:contains("abc")').closest('tr').show();
    } else {
        $('nobr:contains("abc")').closest('tr').hide();
    }
});
});
</script>

Again, not 100% sure, I hope it helps!

  • 1
    Good answer, especially the point that the script should be able to re-run without breaking the behavior. I would disagree on two points though: 1 - you don't need the $(window).on('load') part. An empty function there is doing nothing, and just because you have jQuery reacting to the window load event doesn't change the fact that it also reacts to the document ready event. 2 - You do want to navigate to the tr elements to hide them. The form is laid out as a table, so if you only hide the input elements, you will be left with the field title on the left with no corresponding.... – Dylan Cristy Oct 1 '18 at 12:48
  • 1
    ... input element on the right, which will be confusing. Better to hide the entire row which will hide both the field title and the input element, which emphasizes to the user that they either do or do not need to fill out that piece of metadata. If you see a form that clearly labels the metadata that you need to fill out, and then you see a label for something with no input element to be able to enter data for that, you will think that something is wrong or broken, and be confused. Better to hide it all. – Dylan Cristy Oct 1 '18 at 12:52
  • Thank you so much for both observations. In our environment we typically include the onload, will do some testing regarding this. Regarding the second one, you're actually totally right, I'll remove that bit of info. It confused me that I never prompted fields to actually hide and show, but instead did change the values inherited to those input/select fields. When you hide() it's definitely necessary to address the tr. Thanks again. – Noelia Buniva Oct 1 '18 at 12:55
  • Thank you so much! Of course this is the way I should do it... I'll come back if I have any questions or issues but I'm sure this is going to work. And thank you for the edits from Dylan Cristy - very helpful. – NCCadm Oct 1 '18 at 16:35

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.