Forums

Hide/Show form fields based on form value when form opens

momentis 04 Feb, 2013
Thanks to help in the past from Bob I am using code like the following to show/hide form fields based on the change in a drop-down:

window.addEvent('domready', function() {
  $('trips').addEvent('change', function() {
    if ( $('trips').value == '1' ) {


This works great for changing the values.

However, now I want to open a form and display fields based on a value in the associated record. So, a user has selected "2" when they originally submitted the form. When they open the form to edit the record, I want the form to show the first two sections only, based on the "2" in the number field. Is this possible?
GreyHead 05 Feb, 2013
Hi Rick,

Probably. The script to show or hide the fields needs to run when the page loads as well as on the onChange events:
window.addEvent('domready', function() {
  if ( $('trips').value == '1' ) {
  . . .
  $('trips').addEvent('change', function() {
    if ( $('trips').value == '1' ) {
    . . .

Bob
momentis 05 Feb, 2013
Thanks for that, Bob!! I thought I had tried that, but it looks as if I did not. 😶 It worked. Thanks again for the super help, as usual!!

Rick
laurentmartin 14 Feb, 2013
Hello both,
Can we have more information about how to do this fully.
Can you work this with an example:
Hide a dropdown field when a flag in the session is set to 0 ?
Thanks for your help.
GreyHead 14 Feb, 2013
Hi laurentmartin,

What exactly do you want to do? The session data is only available on the server. I think I'd use Custom code to set either a hidden variable in the form or a JavaScript variable. Then you could use JavaScript to read the value and hide the container div for the select drop-down.

It's easier to reply in detail if you post some specific code.

Bob
laurentmartin 14 Feb, 2013
Hi Bob,
What I am trying to do is to hide a field (let's say a dropdown) face to value stored in database (let's say a flag: if value is 0 then the dropdown will not appear) which will be passed in the session when the form will load.
laurentmartin 15 Feb, 2013
Does anyone know what is te syntax to writte to get a variable or value stored in the session form array to go in JavaScript code ?
GreyHead 15 Feb, 2013
Hi laurentmartin,

In a Load JS action:
var var_name;
var_name = <?php echo $some_value; ?>;

Or better, include this with the script to show/hide the elements.

Bob
laurentmartin 15 Feb, 2013
1 Likes
Hi bob,
Thanks for the help. I was searching for it for long time.
Seems to work, i did this way to test it


var var_name;
var_name = <?php echo $form->data['my_value']; ?>;

alert(var_name);


Now i guess i need to do a if using var_name to setHTML elements to appear or not in functon of the value or var_name ?

Edit:

Here i made it this way, so i share it for other :

window.addEvent('domready', function() {

var var_name;
var_name = <?php echo $form->data['my_value']; ?>;

alert(var_name);

if (var_name == 0){
dropdown_div = $('dropdown_container_div');
dropdown_div.setStyle('display', 'none');
//document.getElementById("dropdown").style.display = 'none'; 
}

else{
dropdown_div = $('dropdown_container_div');
    dropdown_div.setStyle('display', 'block');
}

})



Code need to be inserted in a Load Js action before the form to showup
This topic is locked and no more replies can be posted.