Hide field dependent on other field

This public forum is for user-to-user discussions of ASP.NET Maker. Note that this is not support forum.
Post Reply
Markom
User
Posts: 20

Hide field dependent on other field

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


darkdragon
User
Posts: 150

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


Markom
User
Posts: 20

Post by Markom »

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


Markom
User
Posts: 20

Post 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


darkdragon
User
Posts: 150

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

Markom
User
Posts: 20

Post 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


MichaelG
User
Posts: 1110

Post 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


Post Reply