0

I'm totally new to SP and JS, so sorry in advance for lame question. I find situation similiar to mine here - Conditionally Hide/Show Field on Custom Edit Form but can't make it work.

I have an Edit form where dropdown field (Field A) is existed. There are two other fields (Field B and Field C) which I want to hide from form when Field A is set to specific value.

I can change Fields B and C visibility based on changing Field A using code below (this part works fine) but I also need Fields B and C hidden/shown without changing Field A but based on its current value - when EditForm just initially loads.

<script src="https://code.jquery.com/jquery-1.7.2.min.js" type="text/javascript"> </script> 
<script type="text/javascript">
         $(document).ready(function () {
            $("select[title='Field A']").change(function () {
            console.log("selection changed", $("[title='Field A'] option:selected").text());
                if ($("[title='Field A'] option:selected").text() != "X") {
                    $('nobr:contains("Field B")').closest('tr').hide();
                    $('nobr:contains("Field C")').closest('tr').hide();
                }
                else {
                    $('nobr:contains("Field B")').closest('tr').show();
                    $('nobr:contains("Field C")').closest('tr').show();
                }
            });
        });

</script>

All I need here is Field B and C are hided immediately when form is initially loaded and Field A was already set !=X (without changing Field A).

If you can provide the full code I will highly appreciate it.

0

You could add css style to hide Filed B and Field C.

selector{
display:none
}

Or

    <script src="https://code.jquery.com/jquery-1.7.2.min.js" type="text/javascript"> </script> 
    <script type="text/javascript">
             $(document).ready(function () {
                        $('nobr:contains("Field B")').closest('tr').hide();
                        $('nobr:contains("Field C")').closest('tr').hide();
                $("select[title='Field A']").change(function () {
                console.log("selection changed", $("[title='Field A'] option:selected").text());
                    if ($("[title='Field A'] option:selected").text() != "X") {
                        $('nobr:contains("Field B")').closest('tr').hide();
                        $('nobr:contains("Field C")').closest('tr').hide();
                    }
                    else {
                        $('nobr:contains("Field B")').closest('tr').show();
                        $('nobr:contains("Field C")').closest('tr').show();
                    }
                });
            });
    
    </script>

According to your code, hiding Field B and Filed A when Field A is not equal to X does not match your description.

Updated code:

<script type="text/javascript">
         $(document).ready(function () {
            showOrHide() ;
            $("select[title='Field A']").change(function () {
                showOrHide();
                
            });
        });
function showOrHide(){
    if ($("[title='Field A'] option:selected").text() != "X") {
                    $('nobr:contains("Field B")').closest('tr').hide();
                    $('nobr:contains("Field C")').closest('tr').hide();
                }
                else {
                    $('nobr:contains("Field B")').closest('tr').show();
                    $('nobr:contains("Field C")').closest('tr').show();
                }
}
</script>
4
  • Thanks for your answer. Your code just hide Fields B and C by default and open it if you change Field A. What I need is Fields B and C to be hidden or shown based on existing value in Field A initially, when the EditForm loads, without changing Field A. I need both functionality - it should be shown/hidden when Field A changed (it works already) but I also need it to be shown/hidden when the form loads, without changing (and this is my question) – Dmitriy Aug 5 '20 at 8:51
  • check the update. – Amos Aug 5 '20 at 9:04
  • It works, huge thanks!!! – Dmitriy Aug 5 '20 at 9:16
  • You could mark my reply as answer.Thank you. – Amos Aug 5 '20 at 9: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.