This topic is locked

Hide field based on value of checkbox

10/18/2017 6:35:35 PM
PHPRunner Tips and Tricks
woodey2002 author

Hi Guys,
I got this code working so thought I would share it with you all.
One and add/edit page you have a checkbox that when a user clicks will show a hidden field based on the click.
You will want the "hidden" field to be hidden on page load and reset/wipe if the users un-checks the checkbox.
This code will deliver this functionality!!!
Add the following code to the to Javascript onload event for your desired add/edit page.
Best wishes

James

//Start.............................................................................
pageObj.hideField("yourHiddenFieldname");
var ctrlHideMe = Runner.getControl(pageid, 'yourCheckboxName');
ctrlHideMe.on('change', function(e) {
if (this.getValue()=='on'){
pageObj.showField("yourHiddenFieldname");
} else {
pageObj.hideField("yourHiddenFieldname");
}
//Control reset

if (this.getValue()=='off'){
}

else {
var ctrlReset = Runner.getControl(pageid, 'yourHiddenFieldname');

ctrlReset.clear();
}})
//End.............................................................................
C
CTom 2/15/2019

Tyvm Woodey, you made my day.

E
Eddy Keuninckx 2/16/2019

Great!

jadachDevClub member 2/16/2019

Thanks for posting. I would like to add a note regarding the edit page.
If a user adds a record and checks the box, we assume there is data in "yourHiddenFieldname"
Now when the user edits that record, we want to show that data, but only if the checkbox was checked. Here is how to do that:
On the edit page before display event add this

pageObject.setProxyValue("yourCheckboxName", values["yourCheckboxName"]);


Then on the edit page on the javascript onload event remove

pageObj.hideField("yourHiddenFieldname");



and substitute with this

if(proxy['yourCheckboxName']==1){

pageObj.showField("yourHiddenFieldname");

}

else

{

pageObj.hideField("yourHiddenFieldname");

}



Finally, on the edit page before record updated event, add this

if (values["yourCheckboxName"] == "0")

{

values["yourHiddenFieldname"] = "";

}
B
Bernie 8/27/2019

This is really useful any thoughts if its possible to apply this to a section
So in a survey for example you could ask additional questions depending on a ticked response?

woodey2002 author 8/27/2019

I have successfully hidden/shown tabs not sections in the past.
https://xlinesoft.com/phprunner/docs/about_tabs_sections_api.htm
I think the API allows us to collapse/expand sections and hide and show tabs.
Give it a go to see what you think as tabs worked great for me.
P.S on xlinesoft there is already a cool pre-made survey template https://xlinesoft.com/survey
Cheers,

J