0

I have 4 fields: 1)Gender__c 2) Date_of_birth__c 3) Age__c 4) Field4__c

My condition is: If (Gender__c='Female' & Age__c>12) Then display the field4__c.

I tried with the following code, It is displaying When I select the gender='female' & if I enter the value manually which meets the condition: age>12 .

I wrote another Javascript with @RemoteAction to update Age__c when I select the Date_of_birth__c.

It is not displaying the field4__c, If the Age__c is updated from date_of_birth__c.

Code is:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script type="text/javascript">
    var displayField4 = function() {
        var genderVal   = $( "[id*='inpFldGender']" ).val();
        var ageVal      = $( "[id*='inpFldAge']" ).val();

        if( genderVal === 'Female' && ageVal > 12 ) {
            $( "[id*='inpFld4']" ).show();
            $( "[for*='inpFld4']" ).show();
        }
        else {
            $( "[id*='inpFld4']" ).hide();
            $( "[for*='inpFld4']" ).hide();
        }
    };

    displayField4();
</script>

0

Assuming your field update is called from the onchange event, you need to trigger it manually if you update fields via javascript. jQuery has the change() function to do this.

Also, your function can be made quite a bit simpler with toggle():

var displayField4 = function() {
    var genderVal   = $( "[id*='inpFldGender']" ).val();
    var ageVal      = $( "[id*='inpFldAge']" ).val();

    $( "[id*='inpFld4'], [for*='inpFld4']" ).toggle(genderVal === 'Female' && ageVal > 12);
};
3
  • I tried with above code. Not displaying. – Ravindar Admin Jun 10 '17 at 12:57
  • Have you verified your displayField4 function is actually called by the RemoteAction callback? – IllusiveBrian Jun 10 '17 at 13:05
  • Are you using the correct selector $( "[id$='inpFld4']" ).hide();. if you are giving id to an apex VF tag use id$ instead of id* since apex by default adds form id to the id content if its inside a form. your id that you gave will be the last part of the id. – RedDevil Jun 10 '17 at 13:45

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.