Hide field dependent on other field

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?

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?)

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

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

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);
            }
	}
});

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

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