Page 1 of 1

Hide field dependent on other field

Posted: Tue Jun 20, 2023 9:20 pm
by Markom

I want to hide a field dependent of the value of an other field.

I use a client side event at the specific field:

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

But how do I manage to do so when the view or edit page is loaded without the change function?


Re: Hide field dependent on other field

Posted: Tue Jun 20, 2023 9:27 pm
by darkdragon

You add the whole function you jut wrote on document.ready function:

$(document).ready(function(){
	if ($("#ID_of_selector").val()) {
	// write your code here
	}
});

and place this in the StatupScript of your page (I assumeit's on Edit, right?)


Re: Hide field dependent on other field

Posted: Wed Jun 21, 2023 10:17 pm
by Markom

Yes, it is on edit but also on view. So far no succes.


Re: Hide field dependent on other field

Posted: Thu Jun 22, 2023 2:30 am
by Markom

I did add:

$(document).ready(function(){
	if ($("#ID_of_selector").val()) {
	// write your code here

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

Without succes . I'm not familiar with this technique, so any help is welcome


Re: Hide field dependent on other field

Posted: Thu Jun 22, 2023 5:02 pm
by darkdragon

You should add the code this way.
Replace #ID_of_selector in code with the ID of your element.
Might not work if you have a select element which loads its elements with API

$(document).ready(function(){
	if ($("#ID_of_selector").val()) {
            if (this.value == 2) {
                $(this).fields("Tests_1").visible(false); // true to show, false to hide
                $(this).fields("Tests_2").visible(true);
            } else {
                $(this).fields("Tests_1").visible(true);
                $(this).fields("Tests_2").visible(false);
            }
	}
});

Re: Hide field dependent on other field

Posted: Tue Jun 27, 2023 11:49 pm
by Markom

No success, I tried "Test" and (This).Fields("Test") .

The field Test has either a value 1 or 2, hard coded as option in AspNetMaker


Re: Hide field dependent on other field

Posted: Thu Jun 29, 2023 7:28 am
by MichaelG

Press F12 -> Console in browser, check if there is any error in your codes. To debug, press F12 -> Source, add a break point to your codes, and press F10 to step through the codes to see why it is not working.

Also read:
https://hkvforums.com/viewtopic.php?t=49243