Hide/Show field on select (v2022)

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
philmills
User
Posts: 535

Hide/Show field on select (v2022)

Post by philmills »

I'm using Client side events on a Select field to show/hide fieldname "Trainer".
I modified the example given here

The default value for my field is "1" (as that's the most commonly required option).

When I select value "2", the field "Trainer" becomes invisible as expected.
However, if i then change the selection to either 1 or 3 the fields do not become visible again, and in fact the select drop-down still shows value 2 as highlighted.
What am I doing wrong?

{ // keys = event types, values = handler functions
	"change": function(e) {
		// Your code
if (this.value = "2") {
        	$(this).fields("Trainer").visible(false); // true to show, false to hide
        } else {
        	$(this).fields("Trainer").visible(true);
        }		
	}
}

v2022.12.4


arbei
User
Posts: 9286

Post by arbei »

philmills wrote:

if (this.value = "2") {

Comparison operatior in JavaScript is "==", not "=".


philmills
User
Posts: 535

Post by philmills »

Great - thank-you!


philmills
User
Posts: 535

Post by philmills »

I'm having further problems with this.
When adding a new record, it works fine, but when editing an existing record, the stored value doesn't affect the visible fields...
Basically all fields are shown by default when in fact some should be hidden.
I can only get the right fields to show by selecting a different value from the lookup, then reselecting the stored value.
How can I insert the stored value into the function on edit?


arbei
User
Posts: 9286

Post by arbei »

Your event handler is for the "change" event which only fires when the value is changed by user. If you want to hide fields on page load, you should trigger the change event in Startup Script.


philmills
User
Posts: 535

Post by philmills »

arbei wrote:

Your event handler is for the "change" event which only fires
when the value is changed by user. If you want to hide fields on page load,
you should trigger the change event in Startup Script.

How exactly?
I tried adding the same code to Startup script > Edit page, but it does nothing. I don't know how to convert that code to work in the startup


arbei
User
Posts: 9286

Post by arbei »

Your code is only the handler of the "change" event, it does not run by itself. You need to trigger the handler, you may use .trigger() or .triggerHandler().


philmills
User
Posts: 535

Post by philmills »

I'm a total jquery newbie, I have searched for examples on this forum and the closest I have found is this:
viewtopic.php?p=161242&hilit=trigger#p161242

This is my code currently, but it does nothing:

$("#x_fk_ApplicationType").change(function() {
    if (this.value == "1"){ 
                            $(this).fields("Trainer").visible(true); // true to show, false to hide
                            $(this).fields("Files").visible(true); // true to show, false to hide
                            $(this).fields("URL").visible(true); // true to show, false to hide
                            $(this).fields("Location").visible(true); // true to show, false to hide
                            $(this).fields("Cost").visible(true); // true to show, false to hide
                            $(this).fields("DuringWork").visible(true); // true to show, false to hide
                            $(this).fields("LevelTraining").visible(true); // true to show, false to hide
			    $(this).fields("Agreement").visible(true); // true to show, false to hide
                            $(this).fields("Participants").readonly(true);
                        }        
                if (this.value == "2"){ 
                            $(this).fields("Trainer").visible(false); // true to show, false to hide
                            $(this).fields("Files").visible(false); // true to show, false to hide
                            $(this).fields("URL").visible(false); // true to show, false to hide
                            $(this).fields("Location").visible(false); // true to show, false to hide
                            $(this).fields("Cost").visible(false); // true to show, false to hide
                            $(this).fields("DuringWork").visible(false); // true to show, false to hide
                            $(this).fields("LevelTraining").visible(false); // true to show, false to hide
    			    $(this).fields("Agreement").visible(false); // true to show, false to hide
                            $(this).fields("Participants").readonly(true);
                        }
                if (this.value == "3"){
                            $(this).fields("Trainer").visible(true); // true to show, false to hide
                            $(this).fields("Files").visible(true); // true to show, false to hide
                            $(this).fields("URL").visible(true); // true to show, false to hide
                            $(this).fields("Location").visible(true); // true to show, false to hide
                            $(this).fields("Cost").visible(true); // true to show, false to hide
                            $(this).fields("LevelTraining").visible(true); // true to show, false to hide
    			    $(this).fields("Agreement").visible(true); // true to show, false to hide
                            $(this).fields("DuringWork").visible(false);
                            $(this).fields("Participants").disabled(false);
            }
});
$("#x_fk_ApplicationType").trigger("change");
};

arbei
User
Posts: 9286

Post by arbei »

arbei wrote:

Your code is only the handler of the "change" event, it does not run by itself. You need to trigger the handler, you may use .trigger() or .triggerHandler().

e.g.

$("#x_fk_ApplicationType").triggerHandler("change");

philmills
User
Posts: 535

Post by philmills »

Awesome! Thanks so much, its working now


philmills
User
Posts: 535

Post by philmills »

For one of my select option I'd like to prepopulate my EventTitle field with text.
i can do it with hard text like this:

$(this).fields("EventTitle").value("My Awesome Event Title"); 

but the displayed text really should be dynamic. I have four options which i would be happy to use (in order of preference):
a) insert the viewvalue of the selected dropdown option
b) insert a php variable
c) insert a php function
d) insert a language variable from LanguageLoad

What would be the syntax for any/all of those choices


arbei
User
Posts: 9286

Post by arbei »

You may use any one, I personally would prefer option "d".


mobhar
User
Posts: 11660

Post by mobhar »

philmills wrote:

d) insert a language variable from LanguageLoad

What would be the syntax for any/all of those choices

viewtopic.php?t=54324


Post Reply