Announcements
Hi All,
I have created a simple JavaScript function that shows/hides a field based on an optionset value. It works fine until it is being set as displayed for the first time, once it is displayed(visible), then unable to not display the field, even though I am setting the setVisible to false.
I have debugged the code, and all the values are passing fine, only the issue is the department is being displayed (while it is supposed to be hidden).
Here is the code:
Hi Nadiu,
I'm so glad to hear that you've resolved the issue.
Thanks for your sharing :-)
Have a nice day!
Best Regards,
Leo
Issue is fixed after removing the if (bool) condition in the last part of the code.
Hi Leo,
I have triggered in onchange and given the screenshots.
I fixed the problem.
It was a small error in the code, the if (bool) part, which was not required. I removed that piece and it is working as expected.
Thanks for the inputs.
Hi partner,
I saw that you added the method in onload event which means this event will only be triggered once.
You should add this event to the option set field onChange event and remove the setTimeOut method and try again.
Regards,
Leo
Default: visibility is set to not visible. And when value chosen in category, working as expected.
Similarly, when the required option is chosen, the Department field is set to visible.
But again when the previous or any other option in which the department is supposed to be hidden, the field remains visible.
The issue is, script is triggered, values are passed, but only that the function is not working as expected. No errors.
Hi Leo,
Thanks, I have already done the same.
Made it invisible by default and enabling it only when required. But if user selected the option that makes it visible, the script is working fine as expected, then again if another value is selected in which it should be hidden, the script is not hiding the field. I will update with screenshots so that it is clear.
Any suggestions welcome.
Hi partner,
Since you want set a field visible only when we select a special value in option set, you should set the field unvisible in default.
If so, when the option does not meet the requirement(equals 9797300002 in your case), the field will not display on the form.
BTW, you could simplify your code like the following sample.
function showField(executionContext){ var formContext=executionContext.getFormContext(); var option=formContext.getAttribute("new_option").getValue(); if(option!=null&&option==1){ formContext.getControl("new_showhide").setVisible(true); } else{ formContext.getControl("new_showhide").setVisible(false); } }
I tired in my instance and it worked well.
In additional, you could also use business rule to do this without developing.
Best Regards,
Leo
André Arnaud de Cal...
294,217
Super User 2025 Season 1
Martin Dráb
232,978
Most Valuable Professional
nmaenpaa
101,158
Moderator